textliner.blogg.se

Python random list generator
Python random list generator










python random list generator

$ python2.7 -m timeit -s 'from random import random' '' $ python2.7 -m timeit -s 'from random import sample' '' (Of course this won't beat NumPy.) $ python2.7 -m timeit -s 'from random import randrange' '' To generate 10,000 numbers in the range using sample 10 times. Also, use xrange as the argument to sample, not range. The upper limit of randint is inclusive.įor efficiently, randint is simply a wrapper of randrange which calls random, so you should just use random. Note that randint is very different from random.sample (in order for it to work in your case I had to change the 1,000 to 10,000 as one of the commentators pointed out - if you really want them from 0 to 1,000 you could divide by 10).Īnd if you really don't care what distribution you are getting then it is possible that you either don't understand your problem very well, or random numbers - with apologies if that sounds rude.įirstly, you should use randrange(0,1000) or randint(0,999), not randint(0,1000). T3 = timeit.Timer('nprnd.randint(1000, size=10000)', 'import numpy.random as nprnd') # v3 # Change v2 so that it picks numbers in (0, 10000) and thus runs. It is not entirely clear what you want, but I would use : import numpy.random as nprnd import random randlist Random sequences buffer def generaterrandomlist. By the way, all random numbers are integers. My solution is generating a random number randnum from (0, sumseq) at first, then draw another number randomly from (0, sumseq - randnum). Is there a fast, efficient solution that works at that scale? Some results from the answer The output is a random sequence, and the input is the sum of the sequence. ValueError: sample larger than population V2 is faster than v1, but it is not working on such a large scale. T1 = timeit.Timer('', 'import random') # v1 Here's my current solution: import random I know generating random numbers is a time-consuming task, but there must be a better way.

python random list generator

The distribution of the numbers is not important. I'd like to create a random list of integers for testing purposes.












Python random list generator