This Python exercise will help you to practice and master 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?**

- Now it has
**10 exercise questions**. - 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 **Online Code Editor

**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: Python get a random number within a range

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

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

Reference article for help:

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

Reference article for help: random.choice()

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

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

#### 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: Generate a random float number between a float range

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

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

Ali

I think I may have found a simpler solution for Q10 as yours was difficult to follow:

Vitaliy

Q5

Bill Hardwick

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

Bill Hardwick

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.

Bill Hardwick

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

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