I'm looking for a way to generate a random string of n bytes in Python in a similar way to os.urandom()
method except providing a way to seed the data generation.
So far I have:
def genRandData(size):
buf = chr(random.randint(0,255))
for i in range(size-1):
buf = buf + chr(random.randint(0,255))
return str(buf)
However this function is very slow, generating a megabyte of data takes about 1.8 seconds on my machine. Is there any way of improving this (or alternatively a way to seed os.urandom).
Initialize an empty string and name it as “ran”. Choose the size of the string to be generated. Now using Next () method generate a random number and select the character at that index in the alphabet string. Append that character to randomString. Repeat steps 4 and 5 for n time where n is the length of the string.
The secret is to use explicit seeds for the random function, so that when the test is run again with the same seed, it produces again exactly the same strings. Here is a simplified example of a function that generates object names in a reproducible manner:
This article introduces to you an easy way to generate random strings in Node.js using the randomBytes API provided by the crypto module (a built-in module and no installation required). The output will look like this: Keep in mind that the output contains random strings, so it will be different each time you execute your code.
Yeah posted my question here stackoverflow.com/questions/58854667/… When generating random data, specially for test, it is very useful to make the data random, but reproducible. The secret is to use explicit seeds for the random function, so that when the test is run again with the same seed, it produces again exactly the same strings.
If you have numpy
available, it has a version of the random
module as numpy.random
that contains this function that you might consider:
numpy.random.bytes(length)
It is very fast:
$ python -mtimeit "import numpy" "numpy.random.bytes(1<<30)"
10 loops, best of 3: 2.19 sec per loop
That's for 1GiB.
And you can seed it with numpy.random.seed
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With