PPS Practical 5
PPS Practical 5
import random
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
Example
import 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.
random.randint(a, b)
where,
This yields a number N in the inclusive range [a,b], implying that a <=
N <= b, where the endpoints are included.
Example
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.
random.choice(iterable)
where,
Example
Output
Random number from list: 8
Random number from string: 3
#loop
while roll_again == "yes" or roll_again == "y":
print("Rolling The Dices...")
print("The Values are :")
#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
Output
enter the length of password6
3(xlF%