Python random Module
Example
Generate a random number between 1 and 10:
import random
num = random.randint(1, 10)
print(f'Random number: {num}')
Try it Yourself »
Definition and Usage
The random module generates pseudo-random numbers for various distributions and operations.
Use it to generate random integers, floats, make random selections from sequences, shuffle lists, or create random samples.
Note: Do not use this module for security or cryptographic purposes. Use the secrets
module instead for security-related tasks.
Members
Member | Description |
---|---|
betavariate() | Generate a random number from the beta distribution. |
choice() | Return a random element from a non-empty sequence. |
choices() | Return a list of random elements with replacement. |
expovariate() | Generate a random number from the exponential distribution. |
gammavariate() | Generate a random number from the gamma distribution. |
gauss() | Generate a random number from the Gaussian (normal) distribution. |
getrandbits() | Return a non-negative integer with k random bits. |
getstate() | Return an object capturing the current state of the generator. |
lognormvariate() | Generate a random number from the log normal distribution. |
normalvariate() | Generate a random number from the normal (Gaussian) distribution. |
paretovariate() | Generate a random number from the Pareto distribution. |
randint() | Return a random integer N such that a <= N <= b. |
random() | Return a random float in the range [0.0, 1.0). |
Random | Class that implements the default pseudo-random number generator. |
randrange() | Return a randomly selected element from range(start, stop, step). |
sample() | Return a list of unique random elements from a population. |
seed() | Initialize the random number generator. |
setstate() | Restore the internal state of the generator. |
shuffle() | Shuffle a sequence in place. |
SystemRandom | Class using os.urandom() for cryptographically secure random numbers. |
triangular() | Generate a random number from the triangular distribution. |
uniform() | Return a random floating point number between a and b. |
vonmisesvariate() | Generate a random number from the von Mises distribution. |
weibullvariate() | Generate a random number from the Weibull distribution. |