[go: up one dir, main page]

0% found this document useful (0 votes)
19 views16 pages

Pseudo

The document provides examples of using the random module in Python to generate random numbers, characters, strings, and lottery tickets. It includes exercises demonstrating how to generate random integers within a given range, randomly select characters from a string, create random passwords and numbers meeting certain criteria, roll dice to always get the same number, and create a guessing game with a random target number.

Uploaded by

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

Pseudo

The document provides examples of using the random module in Python to generate random numbers, characters, strings, and lottery tickets. It includes exercises demonstrating how to generate random integers within a given range, randomly select characters from a string, create random passwords and numbers meeting certain criteria, roll dice to always get the same number, and create a guessing game with a random target number.

Uploaded by

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

Pseudo-random numbers

Random module
Practical Part
Problem1

• Generate 3 random integers between 100 and 999 which


is divisible by 5

import random

print("Generating 3 random integer number between 100 and 999 divisible by 5")
for num in range(3):
print(random.randrange(100, 999, 5), end=', ')
• Exercise 2: Random Lottery Pick. Generate 100 random
lottery tickets and pick two lucky tickets from it as a
winner.
import random

lottery_tickets_list = []
print("creating 100 random lottery tickets")
# to get 100 ticket
for i in range(100):
# ticket number must be 10 digit (1000000000, 9999999999)
lottery_tickets_list.append(random.randrange(1000000000, 9999999999))
# pick 2 luck tickets
winners = random.sample(lottery_tickets_list, 2)
print("Lucky 2 lottery tickets are", winners)
• Exercise 4: Pick a random character from a given String

import random

name = 'pynative'
char = random.choice(name)
print("random char is ", char)
• Exercise 9: Roll dice in such a way that every time you
get the same number
• Dice has 6 numbers (from 1 to 6). Roll dice in such a way
that every time you must get the same output number. do
this 5 times.
import random

dice = [1, 2, 3, 4, 5, 6]
print("Randomly selecting same number of a dice")
for i in range(5):
random.seed(25)
print(random.choice(dice))
Exercise 1

The program generates a set number of passwords and includes


a smart setting for the length of the password, as well as which
characters need to be included in it and which ones to exclude.
The program should request the following information from the user:

Number of passwords to generate;


The length of one password;
Should I include the numbers 0123456789?
Should I include uppercase letters ABCDEFGHIJKLMNOPQRSTUVWXYZ?
Do I include lowercase letters abcdefghijklmnopqrstuvwxyz?
Whether to include symbols !#$%&*+-=?@^_?
Should I exclude ambiguous il1Lo0O characters?
Exercise 1

Generate 5 random integers between 1


and 105 which is divisible by 7
Exercise 2

Random Lottery Pick. Generate 100


random lottery tickets and pick two lucky
tickets from it as a winner
Exercise 3

Pick a random character from a


given String
Exercise 4

Generate random String of


length 5
Note: String must be the combination of the UPPER case
and lower case letters only. No numbers and a special
symbol.
Exercise 5

Generate a random Password


which meets the following
conditions
Password length must be 10 characters long.
It must contain at least 2 upper case letters, 1 digit, and 1
special symbol.
Exercise 6

Calculate multiplication of two


random float numbers

First random float number must be between 0.1 and 1


Second random float number must be between 9.5 and 99.5
Exercise 6

Calculate multiplication of two


random float numbers

First random float number must be between 0.1 and 1


Second random float number must be between 9.5 and 99.5
Exercise 7

Roll dice in such a way that


every time you get the same
number
Dice has 6 numbers (from 1 to 6). Roll dice in such a way that every
time you must get the same output number. do this 5 times.
Exercise 8

Random Lottery Pick. Generate 100


random lottery tickets and pick two
lucky tickets from it as a winner.
The lottery number must be 10 digits long.
All 100 ticket number must be unique.
Generate a random list of 1000 numbers using randrange() and
then use the sample() method to pick lucky 2 tickets.
Exercise 9

The program generates a random number in the range from


11 to 100 and asks the user to guess this number. If the
user's guess is greater than a random number, then the
program should output the message 'Too much, try again'. If
the guess is less than a random number, then the program
should output the message 'Too little, try again'. If the user
guesses the number, the program should congratulate him
and display a message 'You guessed it, congratulations!'

You might also like