Fastest Way to generate 1,000,000+ random numbers in python

后端 未结 6 712
时光取名叫无心
时光取名叫无心 2020-12-13 19:28

I am currently writing an app in python that needs to generate large amount of random numbers, FAST. Currently I have a scheme going that uses numpy to generate all of the n

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 20:03

    EDIT Created functions that return the full set of numbers, not just one row at a time. EDIT 2 Make the functions more pythonic (and faster), add solution for second question

    For the first set of numbers, you might consider numpy.random.randint or numpy.random.uniform, which take low and high parameters. Generating an array of 7 x 1,000,000 numbers in a specified range seems to take < 0.7 second on my 2 GHz machine:

    def LimitedRandInts(XLim, N):
        rowlen = (1,N)
        return [np.random.randint(low=0,high=lim,size=rowlen) for lim in XLim]
    
    def LimitedRandDoubles(XLim, N):
        rowlen = (1,N)
        return [np.random.uniform(low=0,high=lim,size=rowlen) for lim in XLim]
    
    >>> import numpy as np
    >>> N = 1000000 #number of randoms in each range
    >>> xLim = [x*500 for x in range(1,8)] #convenient limit generation
    >>> fLim = [x/7.0 for x in range(1,8)]
    >>> aa = LimitedRandInts(xLim, N)
    >>> ff = LimitedRandDoubles(fLim, N)
    

    This returns integers in [0,xLim-1] or floats in [0,fLim). The integer version took ~0.3 seconds, the double ~0.66, on my 2 GHz single-core machine.

    For the second set, I used @Joe Kingston's suggestion.

    def SumToOneRands(NumToSum, N):
        aa = np.random.uniform(low=0,high=1.0,size=(NumToSum,N)) #13 rows by 1000000 columns, for instance
        s = np.reciprocal(aa.sum(0))
        aa *= s
        return aa.T #get back to column major order, so aa[k] is the kth set of 13 numbers
    
    >>> ll = SumToOneRands(13, N)
    

    This takes ~1.6 seconds.

    In all cases, result[k] gives you the kth set of data.

提交回复
热议问题