Purpose/Objectives: Teach student how to implement randomness into their code to make their code simulate real life situations.

In this lesson students will learn:

  • How to import random to python
  • How to use random with a list or number range
  • How to code randomness in everyday scenarios

ADD YOUR ADDITIONAL NOTES HERE:

What are Random Values?

Random Values are a number generated using a large set of numbers and a mathematical algorithm which gives equal probability to all number occuring

Each Result from randomization is equally likely to occur Using random number generation in a program means each execution may produce a different result

What are Examples of Random outputs in the world? Add a few you can think of.

  • Ex: Marbles

Why do we need Random Values for code?

Random values are good for generating random numbers, which can be used in statistics to generate randomly selected groups that eliminate bias. Random values are also good for generating urls (more specifically, urls for sites such as Youtube where each video needs a random number at the end of its address).

Random values can be used in coding:

import random
random_number = random.randint(1,100)
print(random_number)
87
def randomlist():
    list = ["apple", "banana", "cherry", "blueberry"]
    element = random.choice(list)
    print(element)
randomlist()
banana

Real Life Examples: Dice Roll

import random
for i in range(3):
    roll = random.randint(1,6)
    print("Roll " + str(i + 1) + ":" + str(roll))
Roll 1:4
Roll 2:4
Roll 3:4

Challenge #1

Write a function that will a simulate a coinflip and print the output

import random
def coinflip():
    flipCoin = random.choice(["Heads", "Tails"])
    print(flipCoin)

coinflip()
Tails

EXTRA: Create a function that will randomly select 5 playing Cards and check if the 5 cards are a Royal Flush

import random
randCardList = ""

randCardList = []

def randCard(start, end, n=8): # function for generating random int
    randCardList = []
    for i in range(n): # in a range identified in the code
        randCardList.append(random.random(start,end)) #randomizes random numbers
    print ("Randomly generated number list:", randCardList) 
    return randCardList

if randCardList = ?????
    print("Royal Flush")

for r in range(1):
    ranNums = randCardList(1, 100, 5) # generates numbers from 1-100, and only 8 of them
    binary = random.choice(randCardList) # a random binary is chosen from the binary list

Homework

Given a random decimal number convert it into number. As extra, convert it to hexidecimal as well.

import random

def printrandom(randNum):
    randNum = random.randint(1,100)
    print ("Randomly generated number:", randNum)
    divValue = randNum
    remainder = 1
    binaryConv = ''
    while divValue > 0:
        remainder = divValue % 2
        divValue = (int)(divValue / 2)
        binaryConv = str(remainder) + binaryConv
    print("Randomly generated number", randNum ,"is", binaryConv,"in number.")
    # (number converter taken from previous homework)
printrandom(1)
Randomly generated number: 36
Randomly generated number 36 is 100100 in binary.