This Python exercise will help you to practice random data generation techniques. This exercise question 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:
- 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 each question.
Table of contents
- Exercise 1: Generate 3 random integers between 100 and 999 which is divisible by 5
- Exercise 2: Random Lottery Pick. Generate 100 random lottery tickets and pick two lucky tickets from it as a winner.
- Exercise 3: Generate 6 digit random secure OTP
- Exercise 4: Pick a random character from a given String
- Exercise 5: Generate random String of length 5
- Exercise 6: Generate a random Password which meets the following conditions
- Exercise 7: Calculate multiplication of two random float numbers
- Exercise 8: Generate random secure token of 64 bytes and random URL
- Exercise 9: Roll dice in such a way that every time you get the same number
- Exercise 10: Generate a random date between given start and end dates
Exercise 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
Show Solution
Exercise 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 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 a random character from a given String
Reference article for help: random.choice()
Show Solution
Exercise 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.
Show Solution
Exercise 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
Show Solution
Exercise 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
Show Solution
Exercise 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
Show Solution
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.
Reference article for help:
Mohammad says
Q7:
You have num1 instead of num2.
alcebytes says
Bill says
This solution would never have a 0 as the first digit. So piecing together strings of 0-9’s is more accurate and chances of a dup are almost 0.
cub8 says
I think your solution of Q10 is unnecessarily too complicated. Here’s my idea of solving this problem:
Ali says
I think I may have found a simpler solution for Q10 as yours was difficult to follow:
Vitaliy says
Q5
Bill Hardwick says
Q10 – I think your solution is unnecessarily complicated. I offer my simpler solution:
Bill Hardwick says
Q6 – Hmm. I hadn’t come across the shuffle function, so rather than code one myself, I arrived at this alternative solution. It is obviously less efficient than your answer, but given the scale of the problem the timing difference is not of practical significance.
Rishikesh Shah says
Exercise 6: I have also similar solution like you.
Bill Hardwick says
Q5 – badly worded. “String must be the combination of the UPPER case and lower case letters only. No numbers and a special symbol.”
Thus can only be interpreted as requiring a string comprised of letters (any case – or arguably a mix of cases) plus a special symbol. (A slightly more difficult problem which I addressed.)
I suggest re-wording it as “The string must contain only upper and/or lower case alphabetic characters..” There is no need for your second sentence.
Bill Hardwick says
Q2 – Sorry, but I’m afraid your solution is incorrect: it does not preclude duplicate ticket numbers being generated. For example, substituting smaller values into your solution for demonstration purposes:
typically results in duplicate-riddled output like this:
creating 10 random lottery tickets
[18, 16, 13, 13, 16, 10, 10, 14, 11, 16]
My solution uses a set rather than a list to achieve the correct result:
which outputs
Drum roll…
and the lucky winners are numbers:
8975225689 and 1386699143
Note also the use of 10000000000 as the top of the range value rather than your 9999999999 because the end-point for the range is exclusive. (The same comment applies to Q1, although this does not affect the outcome.)
vahlo says
Hi, I believe your conclusion about the original solution is correct but your code could be simplified as well. This code has done the trick for me:
I believe as random.sample generates unique choices it can be used for the tickets as well.
Igor says
Yep, the answer to Q2 is incorrect.
Your solution is slick and concise.
Bill’s idea with the set() was smart, too (although sampling from a set is now deprecated).
I’ve solved it in the following way (which is a bit verbose):