Python random data generation Exercise to **master random data generation techniques.**

This random number generation exercise questions is to help Python developer quickly **learn the various techniques of how to generate random numbers and data in Python**. This random number generation exercise and challenge helps you to understand the Python random module, secrets module, and its methods.

**What is included in this exercise?**

- Now it has
**10 exercise question**. - The
**solution is provided at the end of each question**. - Each question contains a specific skill you need to learn.

When you complete each question you get more familiar with random data generation techniques in Python. **Practice Questions using** **Code Editor**

Tips and helpful learning resources are provided for each question, which helps you to solve the exercise. I request you to solve all the questions by yourself. After you are done with your solution, you can refer my answer.

**Refer to the following tutorials to solve the exercise.**

### Question 1: Generate 3 random integers between 100 and 999 which is divisible by 5

**Reference article for help: **

**Marks: 5**

### Question 2: Random Lottery Pick

Lottery game – Generate a 100 Lottery tickets and pick two lucky tickets from it as a winner.

**Note **you must adhere to the following conditions:

- Lottery number must be 10 digits long.
- All 1000 ticket number must be unique.

**Hint**: Generate a random list of 100 numbers. Use

`random.randrange()`

for number generation and `random.sample()`

method to pick lucky 2 tickets.**Reference article for help: **

**Marks: 15**

### Question 3: Generate 6 digit random secure OTP

**Reference article for help: **

**Marks: 5**

### Question 4: Pick a random charcter from a given String

Reference article for help: random.choice()

**Marks: 5**

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

**Marks: 10**

### 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 letter, 2 digits, and 2 special symbols.

Reference article for help:

**Marks: 15**

### Question 7: Calculate mutiplication 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:**

**Marks: 5**

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

Reference article for help:

**Marks: 10**

### Question 9: Roll dice in such a way that every time you get the same number

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

Reference article for help:

**Marks: 15**

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

**Marks: 15**

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:

import random

lottery_tickets_list = []

print(“creating 10 random lottery tickets”)

for i in range(10):

lottery_tickets_list.append(random.randrange(10, 20))

print(lottery_tickets_list)

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:

import random

tickets = set()

while len(tickets) < 100:

tickets.add(random.randrange(1000000000, 10000000000))

print('Drum roll…')

print('and the lucky winners are numbers:')

winners = random.sample(tickets, 2)

print(f'{winners[0]} and {winners[1]}')

;;; 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.)

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

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.

import string, secrets

s = string.ascii_letters + string.digits + string.punctuation

pw = ”

uc = 0

dg = 0

sp = 0

while len(pw) < 10:

z = secrets.choice(s)

if z in string.ascii_uppercase: uc += 1

elif z in string.digits: dg += 1

elif z in string.punctuation: sp += 1

pw += z

if len(pw) == 10:

#Does it meet the criteria? If not, ditch it and try again

if uc < 2 or dg < 2 or sp < 2:

pw = ''

uc = dg = sp = 0

print(pw)

Bill Hardwick says

Q10 – I think your solution is unnecessarily complicated. I offer my simpler solution:

import random, datetime as dt

start_date = dt.date(2019, 1, 1)

print(f’Start of period = {start_date}’)

end_date = dt.date(2019, 12, 25)

print(f’End of period = {end_date}’)

delta = (end_date – start_date).days

r = random.randint(0, delta)

r_date = start_date + dt.timedelta(days = r)

print(f’Random date = {r_date}’)

Start of period = 2019-01-01

End of period = 2019-12-25

Random date = 2019-04-14

Vitaliy says

Q5

print(“”.join(random.choices(string.ascii_letters, k=5)))