Create random list of integers in Python

后端 未结 4 805
温柔的废话
温柔的废话 2020-11-29 20:17

I\'d like to create a random list of integers for testing purposes. The distribution of the numbers is not important. The only thing that is counting is time

4条回答
  •  臣服心动
    2020-11-29 21:00

    All the random methods end up calling random.random() so the best way is to call it directly:

    [int(1000*random.random()) for i in xrange(10000)]
    

    For example,

    • random.randint calls random.randrange.
    • random.randrange has a bunch of overhead to check the range before returning istart + istep*int(self.random() * n).

    NumPy is much faster still of course.

提交回复
热议问题