Fastest way to fill numpy array with random numbers

chew socks picture chew socks · Nov 7, 2013 · Viewed 9.8k times · Source

Is there a faster way to get a numpy array filled with random numbers than the built in numpy.random.rand(count) function? I know that the built in method is using the Mersenne Twister.

I would like to use numpy for monte carlo simulations, and fetching the random numbers is taking a significant portion of the time. A simple example, calculating pi by monte carlo integration with 200E6 random numbers is only processing about 116.8 MB/s through my program. A comprable program written in C++ using xor128() as the generator processes several hundred MB/s.

EDIT: Miscalculated generation rate

Answer

atomh33ls picture atomh33ls · Nov 7, 2013

You could perhaps get a slight increase in performance by reducing the accuracy - if this is acceptable. I did this by using randint and scaling:

Using ipython %%timeit

count =1000

numpy.random.rand(count)

10000 loops, best of 3: 24.3us per loop

numpy.random.randint(0,1000,count)*0.001

10000 loops, best of 3: 21.4us per loop