In this blog post we will experiment using some of the key functions from the **random library**. Many Python games will involve some degree of “randomness” for the game to be more realistic and unpredictable. Flipping a coin, throwing a dice, shuffling a deck of cards, spawning a sprite at a random location, generating a random password are all actions that can be implemented in Python fairly easily using the random library.

So let’s first investigate the most useful functions from this library to find out how to:

- Generate a random number/integer,
- Generate a random letter from the alphabet,
- Select a random value from a list,
- Identify a random index/location from a list,
- Shuffle the values from a list.

We will then apply these techniques to solve a set of mini challenges to generate the following random values:

#### Generating a random integer:

To generate a random number between two values (e.g. between 1 and 10), you will need to use the *random.randint()* function:

1 2 3 |
import random number = random.randint(1,10) print(number) |

#### Generating a random letter from the alphabet: (Using the ASCII code)

To generate a random letter from the alphabet we can use the ASCII code where the ASCII code for letter “A” is 65 and the ASCII code for letter “Z” is 90. To do so we will generate a random number between 65 and 90 to then retrieve the matching ASCCI character using the *chr()* function.

1 2 3 4 |
import random ascii = random.randint(65,90) letter = chr(ascii) print(letter) |

#### Generating a random letter from the alphabet: (Using the random.choice() function)

An alternative approach used to generate a random letter from the alphabet is to randomly select a character from a string using the *random.choice()* function.

1 2 3 |
import random letter = random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ") print(letter) |

#### Selecting a random value from a list: (using a random index)

We can randomly select a value from a list by first generating a random number between 0 and the length of the list -1. We can then use this rnadomlygenerated index to access the value from the list at the given position/index.

1 2 3 4 5 |
import random weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] index = random.randint(0,len(weekdays)-1) day = weekdays[index] print(day) |

#### Selecting a random value from a list: (using the random.choice() function)

The *random.choice()* function can also be used to randomly pick a value from a list

1 2 3 4 |
import random weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] day = random.choice(weekdays) print(day) |

#### Shuffling list:

The *random.shuffle()* function is used to shuffle all the values from a list.

1 2 3 4 |
import random weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] random.shuffle(weekdays) print(weekdays) |

#### Challenge #1: Generating a random IP address:

An IP address consists of **4 numbers between 0 and 255 separated by a dot**. Your task is to write a Python script that generates and outputs a valid random IP address.

#### Challenge #2: Flipping a coin:

Your task is to write a Python script that randomly outputs “Heads” or “Tails”.

#### Challenge #3: Throwing a dice:

Your task is to write a Python script to throw a dice by **randomly generating a value between 1 and 6.**

#### Challenge #4: Generating a random phone number:

Your task is to write a Python script to generates a random phone number consisting of **11 digits**.

#### Challenge #5: Generating a random postcode:

Your task is to write a Python script to generates a random UK postcode in the format: *LetterLetterNumber_NumberLetterLetter*.

#### Challenge #6: Generating a random date:

Your task is to write a Python script to generates a random date in the format: *DD/MM/YYYY*.

#### Challenge #7: Generating a randomly pick a playing card:

Your task is to write a Python script to randomly pick and output a card from a deck of 52 playing cards. The program should output **the suit and value of the card** (e.g. 7 of hearts, Queen of clubs, etc…).

#### Challenge #8: Generating a random password:

Your task is to write a Python script to randomly generate a strong password **mixing lowercase, uppercase, numbers and punctuation signs in any order**.

You can check this post for some extra help on this challenge.

#### Challenge #9: Generating 6 random lottery numbers:

Your task is to write a Python script to randomly generates and outputs **6 unique random numbers between 1 and 50**.

You can check this post for some extra help on this challenge.

#### Solution...

The solution for this challenge is available to full members!Find out how to become a member:

➤ Members' Area