The Random Library

Finally, after many mentions, we are going to talk about the random library! Like Turtle, random is a library with lots of functions that someone created for everyone else to be able to use and implement in their code.

Basically, this library allows you to randomly select an object or number or element from a given set or range. This is really useful for creating small games or anything of the sort.

Fun Fact: a computer can't do anything randomly, so a random number generator or anything else that we think happens randomly is actually generated by taking the computer's timestamp and putting it through a complicated algorithm and using the outputted number to select something. To use, "random" programs seem random, but to the computer, they are not.

We have already spent a lot of time working with this library, but just for fun, we are going to make two more programs using it. The first one is a Shakespearan insults program that just insults you. Here is the code for it:

								
import random

one = ['artless', 'bawdy', 'beslubbering', 'bootless', 'churlish', 'cockered', 'clouted', 'craven', 'currish', 'dankish', 'dissembling', 
    'droning','errant', 'fawning', 'fobbing' 'froward', 'frothy', 'gleeking', 'goatish', 'gorbellied', 'impertinent', 'infectious', 
    'jarring', 'loggerheaded', 'lumpish', 'mammering', 'mangled', 'mewling', 'paunchy', 'pribbling', 'puking', 'puny', 'quailing', 'rank', 
    'reeky', 'roguish', 'ruttish', 'saucy' 'spleeny', 'spongy', 'surly', 'tottering', 'unmuzzled', 'vain', 'venomed', 'villainous', 'warped',
    'wayward', 'weedy', 'yeasty']

two = ['base-court', 'bat-fowling', 'beef-witted', 'beetle-headed', 'boil-brained', 'clapper-clawed', 'clapper-clawed', 'clay-brained',
    'common-kissing','crook-pated', 'dismal-dreaming', 'dizzy-eyed', 'doghearted', 'dread-bolted','earth-vexing', 'elf-skinned', 
    'fat-kidneyed', 'fen-sucked', 'flap-mouthed', 'fly-bitten', 'folly-fallen', 'fool-born', 'full-gorged',
    'guts-griping', 'half-faced', 'hasty-witted', 'hedge-born', 'hell-hated', 'idle-headed', 'ill-breeding', 'ill-nurtured', 
    'knotty-pated', 'milk-livered', 'motley-minded', 'onion-eyed', 'plume-plucked', 'pottle-deep', 'pox-marked', 'reeling-ripe', 
    'ough-hewn', 'rude-growing', 'rump-fed', 'shard-borne', 'sheep-biting', 'spur-galled', 'swag-bellied', 'tardy-gaited', 'tickle-brained',
    'urchin-snouted', 'weather-bitten' ]

three = ['apple-john', 'baggage', 'barnacle', 'bladder', 'boar-pig', 'bugbear', 'bum-bailey', 'canker-blossom', 'clack-dish', 'clotpole',
    'oxcomb', 'codpiece', 'death-token', 'dewberry', 'flap-dragon', 'flax-wench', 'flirt-gill', 'foot-licker', 'fustilarian', 'giglet',
    'gudgeon', 'haggard', 'harpy', 'hedge-pig', 'horn-beast', 'hugger-mugger', 'jolthead', 'lewdster', 'lout', 'maggot-pie', 'malt-worm',
    'mammet', 'measle', 'minnow', 'miscreant', 'moldwarp', 'mumble-news', 'nut-hook', 'pigeon-egg', 'pignut', 'puttock', 'pumpion', 
    'ratsbane', 'scut', 'skainsmate', 'strumpet', 'varlet', 'vassal', 'whey-face', 'wagtail']

print("Thou ", random.choice(one), ' ', random.choice(two), ' ', random.choice(three), "!!")
								
							

In this program, there are three lists: first two have adjectives and the last one has nouns. The program randomly chooses one element from each list and strings them all together to come up with a creative, Shakespearen-style insult!

Random Color Picker

Your goal for this program is super simple: just create a color picker that randomly picks a color from a list and prints it to the user.

  1. Import the random library(this must be its own line)
  2. Create a list of at least 5 colors(remember, they should all be strings)
  3. Pick a random color from the list using the random module
  4. Print that color

Since this program is so simple, I have an extra challenge for you: try doing all the steps with just one line of code (excluding the import statement)!

Either way you do it, this is what your output should look like:

Additional Resources