Python Random module generates random numbers in Python. These are pseudo-random numbers means they are not truly random.
This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc. It is an in-built function in Python.
Applications of Python Random Module
Here are some real-life uses of Python's random module:
- Games (dice rolls, card draws)
- Simulations (traffic, finance, etc.)
- Data sampling
- Machine learning dataset splitting
- Generating passwords or session tokens
- Software testing and stress testing
List of all the functions Python Random Module
There are different random functions in the Random Module of Python. Look at the table below to learn more about these functions:
Function Name | Description |
---|
seed() | Initialize the random number generator |
---|
getstate() | Returns an object with the current internal state of the random number generator |
---|
setstate() | Used to restore the state of the random number generator back to the specified state |
---|
getrandbits() | Return an integer with a specified number of bits |
---|
randrange() | Returns a random number within the range |
---|
randint() | Returns a random integer within the range |
---|
choice() | Returns a random item from a list, tuple, or string |
---|
choices() | Returns multiple random elements from the list with replacement |
---|
sample() | Returns a particular length list of items chosen from the sequence |
---|
random() | Generate random floating numbers |
---|
uniform() | Return a random floating number between two numbers both inclusive |
---|
triangular() | Return a random floating point number within a range with a bias towards one extreme |
---|
betavariate() | Return a random floating point number with beta distribution |
---|
expovariate() | Return a random floating point number with exponential distribution |
---|
gammavariate() | Return a random floating point number with a gamma distribution |
---|
gauss() | Return a random floating point number with Gaussian distribution |
---|
lognormvariate() | Return a random floating point number with a log-normal distribution |
---|
normalvariate() | Return a random floating point number with normal distribution |
---|
vonmisesvariate() | Return a random floating point number with von Mises distribution or circular normal distribution |
---|
paretovariate() | Return a random floating point number with a Pareto distribution |
---|
weibullvariate() | Return a random floating point number with Weibull distribution |
---|
Examples of Random Module
Example 1: Pick a Random Element from a List
Selects a single random item from a list using random.choice().
Python
import random
a = [1, 2, 3, 4, 5, 6]
print(random.choice(a))
Example 2: Using seed() for Reproducible Output
Random numbers depend on the seeding value. For example, if the seeding value is 5 then the output of the below program will always be the same. Therefore, it must not be used for encryption.
The code sets the random number generator's seed to 5 using random.seed(5). It then prints two random floating-point numbers between 0 and 1 using random.random(). The seed makes these numbers the same every time we run the code with a seed of 5.
Python
import random
random.seed(5)
print(random.random())
print(random.random())
Output0.6229016948897019
0.7417869892607294
Example 3: Generate Random Integers in a Range
random.randint() method is used to generate random integers between the given range.
Syntax: randint(start, end)
Python
import random
r1 = random.randint(5, 15)
print(r1)
r2 = random.randint(-10, -2)
print(r2)
Explanation:
- r1 is a andom number between 5 and 15.
- r2 is a random number between -10 and -2.
Example 4: Generate a Random Float Between 0 and 1
A random.random() method is used to generate random floats between 0.0 to 1.
Syntax: random.random()
Python
from random import random
print(random())
Explanation: random() generates a float number in the range [0.0, 1.0).
Example 5: Randomly Select from List, String, and Tuple
Shows how to use choice() with different sequence types.
Python
import random
a = [1, 2, 3, 4, 5, 6]
print(random.choice(a))
s = "geeks"
print(random.choice(s))
tup = (1, 2, 3, 4, 5)
print(random.choice(tup))
Explanation: choice() picks a random element from a list, string, or tuple.
Example 6: Select Multiple Unique Random Items
this example demonstrates the use of sample() to pick multiple items from sequences without repeating.
Syntax: random.sample(sequence, length)
Python
from random import sample
a = [1, 2, 3, 4, 5]
print(sample(a,3))
b = (4, 5, 6, 7, 8)
print(sample(b,3))
c = "45678"
print(sample(c,3))
Output[4, 2, 3]
[4, 7, 8]
['6', '4', '8']
Explanation: sample() returns a list of unique random elements from the input sequence.
Example 7: Shuffle Elements in a List
random.shuffle() method is used to shuffle a sequence (list). Shuffling means changing the position of the elements of the sequence. Here, the shuffling operation is inplace.
Python
import random
a = [1, 2, 3, 4, 5]
random.shuffle(a)
print("After shuffle : ")
print(a)
random.shuffle(a)
print("\nSecond shuffle : ")
print(a)
OutputAfter shuffle :
[1, 4, 2, 5, 3]
Second shuffle :
[1, 4, 3, 5, 2]
Explanation: shuffle() changes the order of elements in the list randomly, in-place.
Related articles:
Similar Reads
Python Random Module
Python Random module generates random numbers in Python. These are pseudo-random numbers means they are not truly random. This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc. It is an in-built function in Python.Appli
6 min read
Python - random.seed( ) method
random.seed() method in Python is used to initialize the random number generator, ensuring the same random numbers on every run. By default, Python generates different numbers each time, but using .seed() allows result reproducibility.It's most commonly used in:Machine Learning- to ensure model cons
4 min read
random.getstate() in Python
random() module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.getstate() The getstate() method of the random module returns an object with the curr
1 min read
random.setstate() in Python
Random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.setstate() The setstate() method of the random module is used in conjugation with the g
2 min read
random.getrandbits() in Python
random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.getrandbits() The getrandbits() method of the random module is used to return an intege
1 min read
randrange() in Python
The randrange() function in Python's random module is used to generate a random number within a specified range. It allows defining a start, stop, and an optional step value to control the selection of numbers. Unlike randint(), which includes the upper limit, randrange() excludes the stop value. Ex
4 min read
randint() Function in Python
randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions one of them being able to generate random numbers, which is randint(). In this article, we will learn about randint in Python.Python randint() Method SyntaxSyntax: randint(sta
6 min read
Python Numbers | choice() function
choice() is an inbuilt function in Python programming language that returns a random item from a list, tuple, or string. Syntax: random.choice(sequence) Parameters: sequence is a mandatory parameter that can be a list, tuple, or string. Returns: The choice() returns a random item. Note:We have to im
1 min read
Python - random.choices() method
The choices() method returns multiple random elements from the list with replacement. Unlike random.choice(), which selects a single item, random.choices() allows us to select multiple items making it particularly useful for tasks like sampling from a population or generating random data.Example:Pyt
3 min read
random.sample() function - Python
sample() is an built-in function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Example:Pythonfrom random import sample a = [1, 2, 3, 4, 5] print(sample(a,3))Output[2, 5
2 min read