Python Random module
Python Random module
The Python Random module is a built-in module for generating random integers in Python.
These numbers occur randomly and does not follow any rules or instructions. We can
therefore use this module to generate random numbers, display a random item for a list or
string, and so on.
The random() Function
The random.random() function gives a float number that ranges from 0.0 to 1.0. There are
no parameters required for this function. This method returns the second random floating-
point value within [0.0 and 1] is returned.
Code
# Python program for generating random float number
import random
num=random.random()
print(num)
Output:
0.3232640977876686
The randint() Function
The random.randint() function generates a random integer from the range of numbers
supplied.
Code
# Python program for generating a random integer
import random
num = random.randint(1, 500)
print( num )
Output:
215
The randrange() Function
The random.randrange() function selects an item randomly from the given range defined by
the start, the stop, and the step parameters. By default, the start is set to 0. Likewise, the
step is set to 1 by default.
Code
# To generate value between a specific range
import random
num = random.randrange(1, 10)
print( num )
num = random.randrange(1, 10, 2)
print( num )
Output:
4
9
The choice() Function
The random.choice() function selects an item from a non-empty series at random. In the
given below program, we have defined a string, list and a set. And using the above choice()
method, random element is selected.
Code
# To select a random element
import random
random_s = random.choice('Random Module') #a string
print( random_s )
random_l = random.choice([23, 54, 765, 23, 45, 45]) #a list
print( random_l )
random_s = random.choice((12, 64, 23, 54, 34)) #a set
print( random_s )
Output:
M
765
54
The shuffle() Function
The random.shuffle() function shuffles the given list randomly.
Code
# To shuffle elements in the list
list1 = [34, 23, 65, 86, 23, 43]
random.shuffle( list1 )
print( list1 )
random.shuffle( list1 )
print( list1 )
Output:
[23, 43, 86, 65, 34, 23]
[65, 23, 86, 23, 34, 43]
Rock-Paper-Scissor Program using Random Module
Code
# import random module
import random
# Function to play game
def start_game():
# Print games rules and instructions
print(" This is Rock-Paper-Scissors! ")
print(" Please Enter your choice: ")
print(" choice 1: Rock ")
print(" choice 2: Paper ")
print(" choice 3: Scissors ")
#To take the user input
choice_user = int(input(" Select any options from 1 - 3 : "))
Syntax
random.seed(a, version)
Parameter Values
Parameter Description
More Examples
Example
Demonstrate that if you use the same seed value twice, you will get the same
random number twice:
import random
random.seed(10)
print(random.random())
random.seed(10)
print(random.random())
0.5714025946899135
0.5714025946899135