Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to fill numpy array with random numbers

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

like image 524
chew socks Avatar asked Oct 20 '22 21:10

chew socks


1 Answers

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
like image 149
atomh33ls Avatar answered Oct 23 '22 09:10

atomh33ls