Random Island
Our first destination will be Random
Island.
Before we set sail, let me just give you a few brief notes about random
.
Firstly, you will more likely to be using the bigger, more extensive and sparkly numpy.random
for your AI/ML needs. I have briefly introduced this somewhere in the NumPy module.
But sometimes you might just want something simple and light for your simple Pythonic needs, and do not want to rely on an external library for this. Small and simple is sometimes better. So this is where Python’s random
module comes in. It is nothing overly fancy, but it gets the job done. And it is smaller to explore too!
Second point: I’ve also talked about the importance of using a random seed number for your AI/ML experiments. Remember – you want to be able to reproduce your experiments! And remember, computers do not generate random numbers, they generate pseudorandom numbers!
Right, first things first - launch an interactive Python interpreter in your terminal.
So, let’s set up our ship and set our compass to point to Random
Island.
>>> import random
And set a seed number so that we can find our way back there again in the future!
Just pick any number. I chose 1883. This was when the book Treasure Island was published.
>>> random.seed(1883)
Now, time to set sail to Random
Island!
[Insert pirate-y barbership quartet music here]
And while we are sailing, have a glance at the treasure map. Here is the official documentation for the random
module
We have finally arrived at Random
Island!
Now, your missions:
Mission #1: I need a random integer.
Generate a random integer between 5 and 55 (both inclusive).
Remember, the answer is right in front of you on the treasure map!
Mission #2: I need a list of random integers.
Great! But I need more numbers. Unfortunately random
doesn’t give us that.
But no worries - there is always list comprehension to the rescue!
Use list comprehension to generate a list of 50 random integers between 5 and 55 (both inclusive).
Mission #3: I need a random odd integer.
Seems like even numbers are bad luck today. Please generate a random, odd-numbered integer between 5 to 55 (both inclusive).
Mission #4: Random floats.
Enough of integers. I need my ship to float and sail smoothly on the high seas. Please generate a random floating point number between 5.0 to 10.0.
Mission #5: Gaussian-who?
That float was too uniform. I need something more normally distributed. Give me a random number drawn from a Gaussian distribution with a mean of 13 and a standard deviation of 2.
Mission #6: Shuffle my playlist
Ok, enough of numbers. Let’s listen to some 90’s music - they are gold!
But I am tired of always listening to “Barbie Girl” right after that cheesy Britney Spears song. Please randomly shuffle my playlist
!
playlist = ["Baby One More Time", "Barbie Girl", "Candle In The Wind",
"Genie In A Bottle", "I Want It That Way", "I Will Always Love You",
"Livin' la Vida Loca", "Macarena", "My Heart Will Go On",
"One Sweet Day"]
Mission #7: Fruit time!
Ok, now I’m hungry. Looks like I have 5 types of fruits in my barrel.
fruits = ["apple", "orange", "banana", "mango", "papaya"]
Please pick 25 pieces of fruits at random from the barrel to feed the crew!
Mission #8: Picky eater!
My crew is complaining. They seem to be fussy eaters.
- Only 1 out of 20 of them like apples (too boring apparently)
- 6 out of 20 like oranges (their mum told them that it protects against scurvy)
- 10 out of 20 of them are bananas about bananas (they think they are monkeys)
- 3 out of 20 of them love mangoes (especially when these are imported from India)
- Nobody seems to like or even know what papayas are!
So, please pick 25 pieces of fruits at random so that it can roughly fit this distribution of fussy eaters. I’m sure we have loads of bananas in there! Does not need to be perfect - some of them have to learn that they can’t always get their first choice!
Mission #9: Treasure chest
Oh we found a treasure chest! It contains 100 pieces of coins – a mix of gold, silver and bronze.
chest = ["gold"]*50 + ["silver"]*30 + ["bronze"]*20
Please pick 25 pieces of coins at random from the chest. Of course, you should not put a coin back once you’ve removed it!
And we’re done!
Congratulations! You have completed your missions. Arr!!
Now, set the sails! We are off to the next island!