How to Choose a Random Number in Python

At some point in your development journey, you might have to generate random numbers in Python. In this tutorial, we will see how to do that.

The Python built-in random module can generate random numbers (e.g. integers or floats) within a specific range. The NumPy library also provides a random module to generate random numbers.

Let’s start with some examples!

How Do You Choose a Random Number Between 0 And 1 in Python?

Python provides a module to generate random numbers: the random module.

The most basic function of the random module is the random function.

Here is what the random function does, it generates a random float between 0.0 and 1.0:

>>> import random
>>> random.random()
0.7854170732801697
>>> random.random()
0.7340120513329158
>>> random.random()
0.5851946653258203         

And what if you want to generate a random float between 0.0 and 10.0?

You can use the random.uniform() function and pass two arguments that define the beginning and the end of the range.

>>> random.uniform(1, 10)
1.6010581832190662
>>> random.uniform(1, 10)
6.788702746057039
>>> random.uniform(1, 10)
8.085425419675126         

Similarly, to generate a random number between 0 and 100 you use the following code.

>>> random.uniform(1, 100)
80.84958257046604
>>> random.uniform(1, 100)
24.326120311951602
>>> random.uniform(1, 100)
41.81256739317393         

How to Generate a Random Float Between 0 and 1 using NumPy

The NumPy library also provides a module to generate random numbers.

Here is how you can generate a random number between 0.0 and 1.0 using Numpy:

>>> import numpy as np
>>> np.random.random()
0.335309649692459
>>> np.random.random()
0.4965360512032966
>>> np.random.random()
0.7790850138688835         

NumPy also provides the uniform() function that we have seen in the previous section for the Python random built-in module.

Here is how you generate a random float number between 0.0 and 10.0 using NumPy:

>>> np.random.uniform(0, 10)
6.811695148603444
>>> np.random.uniform(0, 10)
6.888316097297719
>>> np.random.uniform(0, 10)
1.610517388296695         

Later on, in this tutorial, we will see what else you can do with the NumPy random module.

Now let’s generate random integers…

How to Generate a Random Integer in Python

The randint() function of the Python random built-in module generates a random integer.

Here is what happens when you call the randint() function without arguments:

>>> random.randint()
Traceback (most recent call last):
  File "", line 1, in 
    random.randint()
TypeError: randint() missing 2 required positional arguments: 'a' and 'b'         

According to the error, the randint function expects two arguments. You can get more details using the help function.

>>> help(random.randint)
Help on method randint in module random:         

randint(a, b) method of random.Random instance
    Return random integer in range [a, b], including both end points.

So, the a and b arguments to be passed to the randint function define the range for the integer to be generated.

With the following code, you get back random integers in the range between 1 and 100:

>>> random.randint(1, 100)
11
>>> random.randint(1, 100)
32
>>> random.randint(1, 100)
26         

The built-in random module also provides a function called randrange. Let’s find out the difference from the randint function.

This is the help for it:

randrange(start, stop=None, step=1, _int=) method of random.Random instance
    Choose a random item from range(start, stop[, step]).

Example of numbers generated with randint()

>>> random.randint(1,3)
1
>>> random.randint(1,3)
3
>>> random.randint(1,3)
3         

Example of numbers generated with randrange()

>>> random.randrange(1,3)
1
>>> random.randrange(1,3)
2
>>> random.randrange(1,3)
2         

I have used the examples above to show that the randint function includes both arguments in the range used to generate the random numbers.

On the other side, the randrange function excludes the second argument from the range.

Conclusion

Now you know how to generate random numbers in Python.

The standard approach is to use Python’s random built-in module, and an alternative is the NumPy library.

Also, just a random question… 😀

What do you need random numbers for in your application?

Let me know in the comments!

Leave a Comment