Python Random Module
Python Random module is an in-built module of Python that is used to generate 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.
List of all the functions Python Random Module
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 |
Random in Python Examples
Let’s discuss some common operations performed by this module.
Example: Printing a random value from a list in Python.
Python3
# import randomimport random# prints a random value from the listlist1 = [1, 2, 3, 4, 5, 6]print(random.choice(list1)) |
Output:
2
Example: Creating random numbers with Python seed() in Python.
As stated above random module creates pseudo-random numbers. 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.
Python3
import randomrandom.seed(5)print(random.random())print(random.random()) |
Output:
0.6229016948897019 0.7417869892607294
Generate Random Numbers in Python
random.randint() method is used to generate random integers between the given range.
Syntax: randint(start, end)
Example: Creating random integers
Python3
# Python3 program explaining work# of randint() function# import random moduleimport random# Generates a random number between# a given positive ranger1 = random.randint(5, 15)print("Random number between 5 and 15 is % s" % (r1))# Generates a random number between# two given negative ranger2 = random.randint(-10, -2)print("Random number between -10 and -2 is % d" % (r2)) |
Output:
Random number between 5 and 15 is 7 Random number between -10 and -2 is -9
Generate Random Float numbers in Python
A random.random() method is used to generate random floats between 0.0 to 1.
Syntax: random.random()
Example:
Python3
# Python3 program to demonstrate# the use of random() function . # import randomfrom random import random # Prints random itemprint(random()) |
Output:
0.3717933555623072
Randomly Select Elements from a List in Python
Random sampling from a list in Python (random.choice, and sample)
Example 1: Python random.choice() function is used to return a random item from a list, tuple, or string.
Python3
import random# prints a random value from the listlist1 = [1, 2, 3, 4, 5, 6]print(random.choice(list1))# prints a random item from the stringstring = "geeks"print(random.choice(string))# prints a random item from the tupletuple1 = (1, 2, 3, 4, 5)print(random.choice(tuple1)) |
Output:
2 k 5
Example 2: Python random.sample() function is used to return a random item from a list, tuple, or string.
Syntax: random.sample(sequence, length)
Python3
# import randomfrom random import sample# Select Random number from List# Prints list of random items of given lengthlist1 = [1, 2, 3, 4, 5]print(sample(list1,3))# Select Random number from Tuple# Prints list of random items of given lengthlist2 = (4, 5, 6, 7, 8)print(sample(list2,3))# Select Random number from string# Prints list of random items of given lengthlist3 = "45678"print(sample(list3,3)) |
Output:
[1, 5, 4] [8, 5, 6] ['7', '4', '5']
Shuffle List in Python
A 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.
Syntax: random.shuffle(sequence, function)
Example: Shuffling a List
Python3
# import the random moduleimport random# declare a listsample_list = [1, 2, 3, 4, 5]print("Original list : ")print(sample_list)# first shufflerandom.shuffle(sample_list)print("\nAfter the first shuffle : ")print(sample_list)# second shufflerandom.shuffle(sample_list)print("\nAfter the second shuffle : ")print(sample_list) |
Output:
Original list : [1, 2, 3, 4, 5] After the first shuffle : [4, 3, 5, 2, 1] After the second shuffle : [1, 3, 4, 5, 2]

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.




Please Login to comment...