Generating random decimals and integers in Python, including random(), randrange(), and randint()

Money and Business

Random numbers can be generated using the random(), uniform(), randange(), and randint() functions in the random module of the Python standard library.

The random module is included in the standard library, so no additional installation is required. Of course, you need to import it.

The following information is provided here.

  • random.random()(Floating point number between 0.0 and 1.0)
  • random.uniform()(Any range of floating point numbers)
  • Generate random numbers that follow a normal distribution, Gaussian distribution, etc.
  • random.randrange()(Integer of arbitrary range and step)
  • random.randint()(An integer in any range)
  • Generating a list with random numbers as elements
    • List of random floating-point numbers
    • List of integer random numbers
  • Initialize the random number generator (fix the random number seed)

See the following article on how to randomly extract or sort elements of a list.

random.random() (Floating point number between 0.0 and 1.0)

The random module's function random() generates a random floating-point number of type float that is between 0.0 and 1.0.

import random

print(random.random())
# 0.4496839011176701

random.uniform() (Any range of floating point numbers)

uniform(a, b)The functions of this random module generate random numbers of floating-point number float type in any of the following ranges

  • a <= n <= b
  • b <= n <= a
import random

print(random.uniform(100, 200))
# 175.26585048238275

The two arguments can be either larger or smaller; if they are equal, they will naturally only return that value. The return value is always a float.

print(random.uniform(100, -100))
# -27.82338731501028

print(random.uniform(100, 100))
# 100.0

The argument can also be specified as a float.

print(random.uniform(1.234, 5.637))
# 2.606743596829249

Whether the value of b is included in the range depends on the following rounding, as documented.
a + (b-a) * random.random()

Whether the endpoint value b is in the range or not depends on the floating point rounding in the following equation
a + (b-a) * random()
random.uniform() — Generate pseudo-random numbers — Python 3.10.2 Documentation

Generate random numbers that follow a normal distribution, Gaussian distribution, etc.

The random() and uniform() functions above generate uniformly distributed random numbers, but there are also functions to generate floating point numbers that follow the following distribution.

  • beta distribution:random.betavariate()
  • exponential distribution:random.expovariate()
  • gamma distribution:random.gammavariate()
  • Gaussian distribution:random.gauss()
  • lognormal distribution:random.lognormvariate()
  • normal distribution:random.normalvariate()
  • Von Mises distribution:random.vonmisesvariate()
  • Pareto distribution:random.paretovariate()
  • Weibull distribution:random.weibullvariate()

The parameters of each distribution are specified by arguments. See the official documentation for details.

random.randrange() (Integer of arbitrary range and step)

randrange(start, stop, step)
The function of this random module returns a randomly selected element from the following elements.
range(start, stop, step)

As with range(), the arguments start and step can be omitted. If they are omitted, start=0 and step=1.

import random

print(list(range(10)))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(random.randrange(10))
# 5

The argument step can be set to generate an even or odd random integer, or a random integer that is a multiple of three.

For example, if start is even and step=2, only even integers in the range can be obtained randomly.

print(list(range(10, 20, 2)))
# [10, 12, 14, 16, 18]

print(random.randrange(10, 20, 2))
# 18

random.randint() (An integer in any range)

randint(a, b)
The function of this random module returns the following random integer int.
a <= n <= b
randrange(a, b + 1)Equivalent to this; note that the value of b is also included in the range.

print(random.randint(50, 100))
# print(random.randrange(50, 101))
# 74

Generating a list with random numbers as elements

In this section, we will explain how to use the random module of the standard library to generate a list with random numbers as elements.

List of random numbers with floating-point floats

To generate a list whose elements are floating-point random numbers, combine the random() and uniform() functions with list comprehension notation.

import random

print([random.random() for i in range(5)])
# [0.5518201298350598, 0.3476911314933616, 0.8463426180468342, 0.8949046353303931, 0.40822657702632625]

In the example above, range() is used, but lists and tuples are also possible for the desired number of elements. For more details on list comprehensions, please refer to the following article.

List of integer int random numbers

When generating a list whose elements are integer random numbers, combining the above randange() and randint() with the list comprehension notation may result in duplicate values.

print([random.randint(0, 10) for i in range(5)])
# [8, 5, 7, 10, 7]

If you want to make a random sequence of integers without duplication, extract the elements of range() with an arbitrary range using random.sample().

print(random.sample(range(10), k=5))
# [6, 4, 3, 7, 5]

print(random.sample(range(100, 200, 10), k=5))
# [130, 190, 140, 150, 170]

For more information about random.sample(), please refer to the following article.

Initialize the random number generator (fix the random number seed)

By giving an arbitrary integer to the random module's function seed(), the random number seed can be fixed and the random number generator can be initialized.

After initialization with the same seed, the random value is always generated in the same way.

random.seed(0)
print(random.random())
# 0.8444218515250481

print(random.random())
# 0.7579544029403025

random.seed(0)
print(random.random())
# 0.8444218515250481

print(random.random())
# 0.7579544029403025
Copied title and URL