Generate Random Numbers (int and float) in Python
In Python, you can generate pseudo-random numbers (int
and float
) with random()
, randrange()
, randint()
, uniform()
, etc., from the random
module.
The random
module is included in the standard library, so no additional installation is required.
See the following articles on how to sample or shuffle elements in a list randomly.
- Random sample from a list in Python (random.choice, sample, choices)
- Shuffle a list, string, tuple in Python (random.shuffle, sample)
See the following article on random number generation with NumPy.
Generate random floating point numbers (float
)
random.random()
: 0.0 <= float
< 1.0
random.random()
generates a random floating point number (float
) within the range of 0.0 <= n < 1.0
.
import random
print(random.random())
# 0.4496839011176701
random.uniform()
: float
in a given range
random.uniform(a, b)
generates a random floating point number (float
) in the range a <= n <= b
or b <= n <= a
.
import random
print(random.uniform(100, 200))
# 175.26585048238275
The two arguments can be in any order, regardless of which is larger or smaller. If they are equal, only that value is returned.
print(random.uniform(100, -100))
# -27.82338731501028
print(random.uniform(100, 100))
# 100.0
The arguments can also be specified as float
values.
print(random.uniform(1.234, 5.637))
# 2.606743596829249
As documented, whether the value of b
is included in the range depends on the rounding equation a + (b-a) * random.random()
.
The end-point value
b
may or may not be included in the range depending on floating-point rounding in the equationa + (b-a) * random()
. random.uniform() — Generate pseudo-random numbers — Python 3.9.7 documentation
Generate random numbers for various distributions (Gaussian, gamma, etc.)
In addition to the uniform distribution provided by random.random()
and random.uniform()
, the random
module also offers functions for generating random numbers from various distributions.
- 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()
- von Mises distribution:
random.vonmisesvariate()
- Pareto distribution:
random.paretovariate()
- Weibull distribution:
random.weibullvariate()
See the official documentation for more information on each distribution.
Generate random integers (int
)
random.randrange()
: int
in a given range and step
random.randrange(start, stop, step)
returns a random integer (int
) within the range(start, stop, step)
.
Similar to range()
, start
and step
can be omitted. If omitted, the default values are 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
You can generate random integers that are either even or odd, or multiples of any integer. For example, if start
is even and step
is set to 2
, only even integers within the range will be randomly generated.
print(list(range(10, 20, 2)))
# [10, 12, 14, 16, 18]
print(random.randrange(10, 20, 2))
# 18
random.randint()
: int
in a given range
random.randint(a, b)
returns a random integer (int
) in the range a <= n <= b
. It is equivalent to random.randrange(a, b + 1)
. Note that the value of b
is included in the range, so it may be generated.
print(random.randint(50, 100))
# print(random.randrange(50, 101))
# 74
Generate a list of random numbers (int
and float
)
List of random floating point numbers
To generate a list of random floating point numbers, you can use functions like random.random()
, random.uniform()
, etc., in combination with list comprehensions.
import random
print([random.random() for i in range(5)])
# [0.5518201298350598, 0.3476911314933616, 0.8463426180468342, 0.8949046353303931, 0.40822657702632625]
See the following article for more information on list comprehensions.
List of random integers
When generating a list of random integers using random.randrange()
or random.randint()
with list comprehensions, it may contain duplicate values.
print([random.randint(0, 10) for i in range(5)])
# [8, 5, 7, 10, 7]
If you want to make a list of random integers without duplicates, you can use random.sample()
to select elements from a range()
.
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]
See the following article for more information about random.sample()
.
random.seed()
: Initialize the random number generator
You can fix the random seed and initialize the random number generator with random.seed()
.
After initializing with the same seed, the same sequence of random numbers will be generated.
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