Random Library Challenges

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:
random-library-tasks

Using the random libraryPython TrinketPython Challenges

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:

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.

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.

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.

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

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.

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

Adapt the code provided to complete the following challenges:

Challenge #1: Generating a random IP address:

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:

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

Challenge #3: Throwing a dice:

random-throw-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:

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:

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:

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:

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

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:

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.

unlock-access

Solution...

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

Did you like this challenge?

Click on a star to rate it!

Average rating 4.3 / 5. Vote count: 26

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!