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: 1) random values are incredibly useful as can be seen in the simulations lecture 2) we can use random values to simulate events such as coinflips or card games. 3) Another way that they are powerful is that they allow us to explore different combinations and probabilities.

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
  • Cryptography
  • Games
  • Statistical Sampling

Why do we need Random Values for code?

Random values can be used in coding:

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

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:2
Roll 2:2
Roll 3:5

Challenge #1

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

def coinflip():
    return "Heads" if random.randint(1,2) == 1 else "Tails"

print(coinflip())
Tails

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

cards = [i for i in range(52)]
def selectCard(cards):
    res = random.choice(cards)
    cards.remove(res)
    return res
deck = [selectCard(cards) for i in range(5)]
if set(deck) == set([0,1,2,3,4]):
    print("Royal Flush Detected")
elif set(deck) == set([5,6,7,8,9]):
    print("Royal Flush Detected")
elif set(deck) == set([10,11,12,13,14]):
    print("Royal Flush Detected")
elif set(deck) == set([15,16,17,18,19]):
    print("Royal Flush Detected")
else:
    print("No Royal Flush")
Royal Flush Detected

Homework

Given a random decimal number convert it into binary as Extra convert it to hexidecimal as well.

def DecimalToHex(num):
    hex_correlation = {
        0:"0",
        1:"1",
        2:"2",
        3:"3",
        4:"4",
        5:"5",
        6:"6",
        7:"7",
        8:"8",
        9:"9",
        10:"A",
        11:"B",
        12:"C",
        13:"D",
        14:"E",
        15:"F"
    }
    res = ""
    digits = 7
    while digits >= 0:
        if (num - 16 ** digits) >= 0:
            sub_res = 0
            while (num - 16 ** digits) >= 0:
                sub_res+=1
                num -= 16 ** digits  
            res = res + str(hex_correlation[sub_res])
        else:
            res = res + "0"
        digits-=1  
    return res


num = random.randint(0,1000000000)
print("Random Number:", num)
print("Binary representation of random number:", bin(num)[2:])
print("Hexadecimal representation of random number:", DecimalToHex(num))
Random Number: 345791430
Binary representation of random number: 10100100111000101101111000110
Hexadecimal representation of random number: 149C5BC6