Skip to content

Generate Random Numbers in Python

Generate random numbers in Python Cover Image

In this tutorial, you’ll learn how to generate random numbers in Python. Being able to generate random numbers in different ways can be an incredibly useful tool in many different domains. Python makes it very easy to generate random numbers in many different ways.

In order to do this, you’ll learn about the random and numpy modules, including the randrange, randint, random, and seed functions. You’ll also learn about the uniform and normal functions in order to create more controlled random values.

By the end of this tutorial, you’ll have learned:

  • How to generate random floating point values and integers
  • How to generate random numbers between different values
  • How to create lists of random numbers
  • How to generate a random number following a gaussian distribution

Generate Random Floating Point Value in Python

Python comes with a package, random, built in. This means that you don’t need to install any additional libraries. The library makes it incredibly easy to generate random numbers. Let’s first see how to create a random floating point between 0 and 1.

# Generate a random float between 0 and 1
import random
print(random.random())

# Returns: 0.9853067530373302

Let’s break down what we did here:

  1. We imported the random library
  2. We used the .random() function from the library

The random() function is used to generate a random float between 0 and 1.

Generate a Random Float Between 2 Numbers

While the random() function generates a random float between 0 and 1. However, there may be times you want to generate a random float between any two values. For this, you can use the .uniform() function. Let’s see how this works:

# Generating a random float between two numbers
import random
print(random.uniform(10, 20))

# Returns: 18.754091000858978

You can see that the random number that’s returned is between (and can include) the boundary numbers.

In the next section, you’ll learn how to generate a random integer in Python.

Generate Random Integer in Python

The random library makes it equally easy to generate random integer values in Python. For this, you can use the randint() function, which accepts two parameters:

  • a= is the low end of the range, which can be selected
  • b= is the high end of the range, which can also be selected

Let’s see how we can generate a random integer in Python:

# Generating a random integer in Python
import random
print(random.randint(0,10))

# Returns: 6

Generate Random Numbers Between Two Values in Python

In the example above, we used 0 as the starting point. However, if we wanted to generate a random number between two values, we can simply specify a different value as the starting point (including negative values).

Let’s repeat this example by picking a random integer between -100 and 100:

# Generating a random integer between two values
import random
print(random.randint(-100,100))

# Returns: -24

Generate Random Numbers Between Two Values at Particular Steps in Python

In this section, you’ll learn how to generate random numbers between two values that increase at particular steps. This means that say you wanted to choose a random number between, say, 0 and 100, but only in multiples of 3.

For this, you can use the randrange() function. Let’s see how this works:

# Choosing a Random Value from a Range
import random
print(random.randrange(0,100,3))

# Returns: 51

The important piece to note here is that the upper limit number is not included in the selection. In order to include it, simply add 1 to the value, such as random.randrange(0, 101, 3).

Generate a List of Random Numbers in Python

In this section, you’ll learn how to generate a list of random numbers in Python. Because this can be done in different ways, we’ll split this section into multiple parts. By the end of this, you’ll learn how to select a list of random floats, random integers, and random integers without repetition.

Generate a List of Random Floats

In order to generate a list of random floats, we can simply call the .random() or .uniform() functions multiple times. We can use either a for loop or a list comprehension.

In the examples below, we’ll use a Python list comprehension to generate two lists: one with random floats between 0 and 1 and the other with random floats between two specified numbers:

# Generate a list of random floats between 0 and 1
import random

random_list = [random.random() for i in range(4)]
print(random_list)

# Returns: [0.5581711161676459, 0.12711142718586899, 0.766167958409145, 0.16922239303028397]

Let’s break down what we did here:

  1. We instantiated a new list, random_list which holds a list comprehension
  2. The list comprehension repeats calling the .random() function 4 times.

We can apply the same approach to create a list of random floats between two given numbers:

# Generate a list of random floats between 10 and 30
import random

random_list = [random.uniform(10, 30) for i in range(4)]
print(random_list)

# Returns: [17.8132149201385, 25.11003157219303, 10.68442113276928, 24.26692625415757]

Generate a List of Random Integers

We can apply the same approach to generate a list of random integers. To do this, we’ll create a list comprehension that calls the random.randint() function multiple times. Let’s see how we can create a list of 5 random integers between 50 and 100:

# Generate a list of random integers in Python
import random

random_list = [random.randint(50, 100) for i in range(5)]
print(random_list)

# Returns: [79, 66, 55, 69, 73]

Generate a List of Random Integers without Substitution

In this section, you’ll learn how to generate a list of random integers without substitution. This means that you can select a number once, and only once. In order to do this, you can use the random.sample() function.

The function expects a list of values and the number of values to select. So, say you wanted to select five values without substitution between 0 and 15, you could write:

# Generate a list of random integers without substitution
import random

random_list = random.sample(range(0, 16), k=5)
print(random_list)

# Returns: [2, 6, 14, 9, 12]

Generate a Random (Normal) Gaussian Distribution in Python

The random library also allows you to select a random value that follows a normal Gaussian distribution. In order to do this, you can use the gauss() function, which accepts both the mean and the standard deviation of the distribution.

Let’s see how you can generate a random value from a distribution with the mean of 10 and a standard deviation of 1:

# Selecting a random number from a Normal Distribution
import random

random_gaussian = random.gauss(10, 1)
print(random_gaussian)

# Returns: 10.202050547919738

Want to create an entire distribution? Check out this article here, which teaches you how to produce an entire Gaussian (Normal) distribution using Numpy.

The random library also comes with helpful ways to generate random numbers from other types of distributions. Check out the full list below:

  • Beta distribution: random.betavariate()
  • Exponential distribution: random.expovariate()
  • Gamma distribution: random.gammavariate()
  • Gaussian distribution: random.gauss()
  • Log normal distribution: random.lognormvariate()
  • Normal distribution: random.normalvariate()

Create Reproducible Random Numbers in Python

There will be many times when you want to generate a random number, but also want to be able to reproduce your result. This is where the random.seed() function come in. This allows you to set a seed that you can reproduce at any time.

Let’s see how we can do this:

# Creating results you can reproduce
import random

random.seed(100)
print(random.random())

# Returns: 0.1456692551041303

Let’s break this down a little bit:

  1. We imported the library
  2. We then instantiated a random seed, using the value of 100
  3. Then when we printed a random float, a value was returned
  4. If we were to run this again, the same value would be returned

Conclusion

In this tutorial, you learned how to generate random numbers using Python. You learned how the random library works to generate random numbers, including floats and integers. You also learned how to generate random numbers from different ranges of numbers, including only multiples of numbers.

You then learned how to generate lists of random numbers, including floats and integers, as well as without substitution. Finally, you learned how to select random numbers from a normal distribution and how to reproduce your results.

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.