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.

**Python Exercises with Solutions**

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

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

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

Reference article for help:

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

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

Reference article for help:

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

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

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

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

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

Vitaliy

Q5

Ali

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