This Python exercise will help you practice random data generation techniques. This exercise focuses on generating random numbers, choices, and samples using the random
module and secrets
module.
Also Read: Python Random Data Generation Quiz
This exercise includes:
- There are 10 coding questions.
- The solution is provided at the end of each question.
- As you complete each question, you will become more familiar with random data generation techniques in Python
Refer to the following tutorials to solve the exercise.
Use Online Code Editor to solve each question.
Table of contents
- Exercise 1: Generate 3 Random Integers
- Exercise 2: Random Lottery Pick
- Exercise 3: Generate 6 digit Random Secure OTP
- Exercise 4: Pick Random Character
- Exercise 5: Generate Random String
- Exercise 6: Generate Random Password
- Exercise 7: Calculate Multiplication
- Exercise 8: Generate Random Token and URL
- Exercise 9: Dice Roll
- Exercise 10: Generate Random Date
Exercise 1: Generate 3 Random Integers
Write a code to generate 3 random integers between 100 and 999 which is divisible by 5
Reference article for help: Python get a random number within a range
Show Solution
Exercise 2: Random Lottery Pick
Write a code to generate 100 random lottery tickets and pick two lucky tickets from it as a winner.
Note you must adhere to the following conditions:
- The lottery number must be 10 digits long.
- All 100 ticket number must be unique.
+ Hint
Generate a random list of 1000 numbers using randrange() and then use the sample() method to pick lucky 2 tickets.
Show Solution
Exercise 3: Generate 6 digit Random Secure OTP
Reference article for help:
Show Solution
Exercise 4: Pick Random Character
Write a code to select a random character from a given string name = 'pynative'
.
Reference article for help: random.choice()
Show Solution
Exercise 5: Generate Random String
Write a code to 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.
Reference article for help: Generate random String in Python.
Show Solution
Exercise 6: Generate Random Password
Write a code to 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.
Reference article for help: Generate random String and Password in Python
Show Solution
Exercise 7: Calculate Multiplication
Write a code to calculate multiplication of two random float numbers.
Note:
- First random float number must be between 0.1 and 1
- Second random float number must be between 9.5 and 99.5
Reference article for help: Generate a random float number between a float range
Show Solution
Exercise 8: Generate Random Token and URL
Write a code to generate random secure token of 64 bytes and random URL.
Reference article for help: Python secrets module to generate a secure token and URL
Show Solution
Exercise 9: Dice Roll
Write a code to 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.
Given:
import random
dice = [1, 2, 3, 4, 5, 6]
Code language: Python (python)
Reference article for help:
Show Solution
Exercise 10: Generate Random Date
Write a code to generate a random date between given start and end dates.
Given:
import random
import time
def getRandomDate(startDate, endDate ):
# Your code here
print ("Random Date = ", getRandomDate("1/1/2016", "12/12/2018"))
Code language: Python (python)