Python random

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()
>>> random.random()
>>> random.random()

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)
>>> random.uniform(1, 10)
>>> random.uniform(1, 10)

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

>>> random.uniform(1, 100)
>>> random.uniform(1, 100)
>>> random.uniform(1, 100)

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()
>>> np.random.random()
>>> np.random.random()

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)
>>> np.random.uniform(0, 10)
>>> np.random.uniform(0, 10)

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 
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)
>>> random.randint(1, 100)
>>> random.randint(1, 100)

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)
>>> random.randint(1,3)
>>> random.randint(1,3)

Example of numbers generated with randrange()

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

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.


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 Reply

Your email address will not be published. Required fields are marked *