The generating speed for random number in Python3

Just want to generate random number in a range (no matter float or integer) by using Python. Since I only need to get a random number in my code once a time, the speed for calling the generating-function is critical.

So let’s do the experiment:

The result is:

Looks the random.uniform() from standard library of Python3 is the fastest one. But there is still a odd phenomenon: numpy is as fast as we expected.
Actually, the correct way of using numpy.random.uniform() is setting its size argument.

The result is:

Thus the best way to generating a bunch of random numbers at a time is numpy.random.uniform()

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.