This Python exercise will help you to practice random data generation techniques. This random number generation exercise question focuses on questions on generating random numbers, choices, and samples.

Further Reading: Explore All Python Exercises and Python Quizzes to practice Python.

This random number generation exercise and challenge helps you to understand the different use of the Python random module, secrets module, and its methods.

What is included in this exercise?

As of now, this exercise contains 10 questions .

. The solution is provided at the end of each question.

When you complete each question, you will be more familiar with random data generation techniques in Python.

Refer to the following tutorials to solve the exercise.

Use Online Code Editor to solve exercise questions.

Question 1: 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

Solution: 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=', ') Show Solution Hide Solution

Question 2: Random Lottery Pick. 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 random.randrange() and then use random.sample() method to pick lucky 2 tickets.

Solution: 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) Show Solution Hide Solution

Question 3: Generate 6 digit random secure OTP

Reference article for help:

Solution: import secrets #Getting systemRandom class instance out of secrets module secretsGenerator = secrets.SystemRandom() print("Generating 6 digit random OTP") otp = secretsGenerator.randrange(100000, 999999) print("Secure random OTP is ", otp) Show Solution Hide Solution

Question 4: Pick a random character from a given String

Reference article for help: random.choice()

Solution: import random name = 'pynative' char = random.choice(name) print("random char is ", char) Show Solution Hide Solution

Question 5: 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.

Solution: import random import string def randomString(stringLength): """Generate a random string of 5 charcters""" letters = string.ascii_letters return ''.join(random.choice(letters) for i in range(stringLength)) print ("Random String is ", randomString(5) ) Show Solution Hide Solution

Question 6: 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

Solution: import random import string def randomPassword(): randomSource = string.ascii_letters + string.digits + string.punctuation password = random.sample(randomSource, 6) password += random.sample(string.ascii_uppercase, 2) password += random.choice(string.digits) password += random.choice(string.punctuation) passwordList = list(password) random.SystemRandom().shuffle(passwordList) password = ''.join(passwordList) return password print ("Password is ", randomPassword()) Show Solution Hide Solution

Question 7: 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

Solution: import random num1 = random.random() print("First Random float is ", num1) num2 = random.uniform(9.5, 99.5) print("Second Random float is ", num1) num3 = num1 * num2 print("Multiplication is ", num3) Show Solution Hide Solution

Question 8: Generate random secure token of 64 bytes and random URL

Reference article for help: Python secrets module to generate a secure token and URL

Solution: import secrets print("Random secure Hexadecimal token is ", secrets.token_hex(64)) print("Random secure URL is ", secrets.token_urlsafe(64)) Show Solution Hide Solution

Question 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.

Reference article for help:

Solution: 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)) Show Solution Hide Solution

Question 10: Generate a random date between given start and end dates