0% found this document useful (0 votes)
38 views8 pages

PPS Practical 5

The random module in Python defines functions for generating pseudo-random numbers. It contains functions like random() for generating random floats between 0 and 1, and randint() for generating random integers within a given range. Some key functions are random.random() which generates a random float, random.randint() which generates a random integer between given lowest and highest values, and random.choice() which selects a random element from a list. The random module is commonly used for applications requiring random numbers, such as simulations, games, simulations, and cryptography.

Uploaded by

dukerhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views8 pages

PPS Practical 5

The random module in Python defines functions for generating pseudo-random numbers. It contains functions like random() for generating random floats between 0 and 1, and randint() for generating random integers within a given range. Some key functions are random.random() which generates a random float, random.randint() which generates a random integer between given lowest and highest values, and random.choice() which selects a random element from a list. The random module is commonly used for applications requiring random numbers, such as simulations, games, simulations, and cryptography.

Uploaded by

dukerhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

==Practical-5

Random Module to Generate Random


Numbers in Python

Suppose a situation arises where we want the computer to choose a


random integer within a defined range, a random element from a list, a
random card from a deck, flip a coin, and so on. For such purposes,
we use the Python import random module. In this article, we will
focus on the Python import random module, along with its different
functions, its programming applications, and a program to generate
random numbers in Python.

Python Random Module

First, we need to import the random module as following:

import random

The Python import random module in Python defines a series of


functions for generating or manipulating random integers. Python
random() is a pseudo-random number generator function that
generates a random float number between 0.0 and 1.0, is used by
functions in the random module.
Because the sequence of numbers created is determined by the seed,
these are pseudo-random numbers. The sequence will be the same if
the seeding value is the same. If you pick 2 as the seeding value, for
example, you will always see the following sequence. These functions
are used in a variety of games, lotteries, and other applications that
require random number creation.

Example

import random

random.seed(5)

print(random.random())

print(random.random())

print(random.random())

Output

0.6229016948897019
0.7417869892607294
0.7951935655656966
Python Program to Generate Random Floats

The Python import random module is also used to generate random


float values. This is done by using random.random() method that helps
generate a random float value ranging from 0.0 to 1.0. There are no
arguments required for the function.

Example

import random

print('Printing random number using random.random():')


print(random.random())

Output
Printing random number using random.random():
0.909824521137017

As you can see from the results, we got 0.90. It’s possible that you’ll
obtain a different number.

● The random module’s most fundamental function is


random.random().
● Almost all of the random module’s functions are dependent on
the fundamental function random().
● random() returns the next random floating-point number
between [0.0, 1.0]
Python Program to Generate Random Integers

In Python, the randint() method is used to generate a random number


between the supplied integers. The random module defines this
function. The syntax of this function is:

random.randint(a, b)

where,

a – starting integer value

b – ending integer value

This yields a number N in the inclusive range [a,b], implying that a <=
N <= b, where the endpoints are included.

Example

# Program to generate a random number


# importing the random module
import random

print('Random number:', random.randint(0,10))


print('Random number:', random.randint(0,50))
print('Random number:', random.randint(0,100))

Output
Random number: 5
Random number: 32
Random number: 84

Note – Every time we run the above program, the Python interpreter
will generate a different number.

Python Program to Generate Random Number


from a List and a String

Python random.choice() is a built-in Python function that returns a


randomly selected item from a list, tuple, or string.

The syntax is as follows:

random.choice(iterable)

where,

iterable – a sequence of list, tuple or a string

If the iterable passed to random.choice() is empty, Python interpreter


will raise an exception.

Example

# Python program to illustrate choice() method


import random
# prints a random value from the list
list1 = [8, 5, 6, 2, 0, 4]
print('Random number from list:', random.choice(list1))

# prints a random item from the string


string = '9674271903'
print('Random number from string:', random.choice(string))

Output
Random number from list: 8
Random number from string: 3

Q1. What is import random in Python?

The random module in Python defines a series of functions for


generating or manipulating random integers. The import random loads
the random module, which contains a number of random number
generation-related functions. Python random() is a pseudo-random
number generator function that generates a random float number
between 0.0 and 1.0, is used by functions in the random module.

Q2. How do you import a random number in Python?

In Python, the randint() method is used to generate a random number


between the supplied integers. The random module defines this
function. The syntax of this function is:
import random

#range of the values of a dice


min_val = 1
max_val = 6

#to loop the rolling through user input


roll_again = "yes"

#loop
while roll_again == "yes" or roll_again == "y":
print("Rolling The Dices...")
print("The Values are :")

#generating and printing 1st random integer from 1 to 6


print(random.randint(min_val, max_val))

#generating and printing 2nd random integer from 1 to 6


print(random.randint(min_val, max_val))

#asking user to roll the dice again. Any input other than yes or y
will terminate the loop
roll_again = input("Roll the Dices Again?")

Output
Rolling The Dices...
The Values are :
4
4
Roll the Dices Again?y
Rolling The Dices...
The Values are :
4
2
Roll the Dices Again?n
Story Generator with Python
import random
when = ['A few years ago', 'Yesterday', 'Last night', 'A long time
ago','On 20th Jan']
who = ['a rabbit', 'an elephant', 'a mouse', 'a turtle','a cat']
name = ['Ali', 'Miriam','daniel', 'Hoouk', 'Starwalker']
residence = ['Barcelona','India', 'Germany', 'Venice', 'England']
went = ['cinema', 'university','seminar', 'school', 'laundry']
happened = ['made a lot of friends','Eats a burger', 'found a secret key',
'solved a mistery', 'wrote a book']
print(random.choice(when) + ', ' + random.choice(who) + ' that lived in '
+ random.choice(residence) + ', went to the ' + random.choice(went) + '
and ' + random.choice(happened))

Output
On 20th Jan, a rabbit that lived in India, went to the school and made a
lot of friends

Python Program to Generate Password


import random
passlen = int(input("enter the length of password"))
s="abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*
()?"
p = "".join(random.sample(s,passlen ))
print(p)

Output
enter the length of password6
3(xlF%

You might also like