I need to generate a huge amount of pseudo random integers in python 3.x .
Because of this I have to find the fastest way possible to do so.
Currently I'm using the python random
library.
I found a similar answer using numpy
at Fastest Way to generate 1,000,000+ random numbers in python but numpy isn't a standard library and i don't want to add 3rd party libraries (if not really needed) because I can't be sure that the guys I'm giving it to have them installed.
Actual code
def randInt(self, size=20, amount=1000):
retVal = list()
for i in range(0, amount):
retVal.append(random.Random._randbelow(size)+1) #+1 to exclude 0 and include size
return retVal
Tested with:
start = time.time()
test = randInt(size=100, amount=1000000)
stop = time.time()
print(stop-start)
These random
functions I've tested:
_randbelow : 1.6700019 sec
randrange : 2.8700041 sec
randint : 3.1200051 sec
So my question is:
Is there any faster way than _randbelow
using python 3.x default libraries?
Thanks
chill0r
Edit
Inbar Roses suggestion tested with same method (to compare results)
uniform : 0.9200019 sec
uniform parsed to int : 1.24000 sec
1.24 sec for 1 million integers should do it for me.
If it should make problems later I probably have to look into multithreading or really using numpy
.