0% found this document useful (0 votes)
4 views

Python Random module

The Python Random module is a built-in module that allows for the generation of random numbers, including integers and floating-point values. Key functions include random(), randint(), randrange(), choice(), and shuffle(), each serving different purposes for generating or manipulating random data. Additionally, the seed() method is used to initialize the random number generator, allowing for reproducibility of random sequences.

Uploaded by

s78093235
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Random module

The Python Random module is a built-in module that allows for the generation of random numbers, including integers and floating-point values. Key functions include random(), randint(), randrange(), choice(), and shuffle(), each serving different purposes for generating or manipulating random data. Additionally, the seed() method is used to initialize the random number generator, allowing for reproducibility of random sequences.

Uploaded by

s78093235
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 : "))

# randint() Function which generates a random number by computer


choice_machine = random.randint(1, 3)

# display the machines choice


print(" Option choosed by Machine is: ", end = " ")
if choice_machine == 1:
print(" Rock ")
elif choice_machine == 2:
print("Paper")
else:
print("Scissors")

# To declare who the winner is


if choice_user == choice_machine:
print(" Wow It's a tie! ")
elif choice_user == 1 and choice_machine == 3:
print(" Congratulations!! You won! ")
elif choice_user == 2 and choice_machine == 1:
print(" Congratulations!! You won! ")
elif choice_user == 3 and choice_machine == 2:
print(" Congratulations!! You won! ")
else:
print(" Sorry! The Machine Won the Game? ")

# If user wants to play again


play_again = input(" Want to Play again? ( yes / no ) ").lower()
if play_again == " yes ":
start_game()
else:
print(" Thanks for playing Rock-Paper-Scissors! ")

# Begin the game


start_game()
Output:
This is Rock-Paper-Scissors!
Please Enter your choice:
choice 1: Rock
choice 2: Paper
choice 3: Scissors
Select any options from 1 - 3 : 1
Option choosed by Machine is: Rock
Wow It's a tie!
Want to Play again? ( yes / no ) yes
This is Javatpoint's Rock-Paper-Scissors!
Please Enter your choice:
choice 1: Rock
choice 2: Paper
choice 3: Scissors
Select any options from 1 - 3 : 2
Option choosed by Machine is: Scissors
Congratulations!! You won!
Want to Play again? ( yes / no ) no
Thanks for playing Rock-Paper-Scissors!

Definition and Usage


The seed() method is used to initialize the random number generator.
The random number generator needs a number to start with (a seed value), to
be able to generate a random number.
By default the random number generator uses the current system time.
Use the seed() method to customize the start number of the random number
generator.
Note: If you use the same seed value twice you will get the same random
number twice. See example below

Syntax
random.seed(a, version)

Parameter Values
Parameter Description

a Optional. The seed value needed to generate a random


number.
If it is an integer it is used directly, if not it has to be
converted into an integer.
Default value is None, and if None, the generator uses the
current system time.

version An integer specifying how to convert the a parameter into a


integer.
Default value is 2

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

You might also like