Fastest way to fill numpy array with random numbers

10,418

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
Share:
10,418
chew socks
Author by

chew socks

Updated on June 28, 2022

Comments

  • chew socks
    chew socks almost 2 years

    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