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:

  • To get access to the random module, we add from random import * to the top of our program
  • 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
  • In Python, Modules are simply files with the “. py” extension containing Python code that can be imported inside another Python Program. In simple terms, we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application.
  • Python Random module is an in-built module of Python which is used to generate random numbers. These are pseudo-random numbers means these are not truly random. This module can be used to perform random actions such as generating random numbers, print random a value for a list or string, etc.

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 can be used in coding:

import random
random_number = random.randint(1,100)
print(random_number)
3
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:1
Roll 2:2
Roll 3:1

Challenge #1

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

import random
def coinflip():

    a = random.randint(0, 1)
    pos = ["Heads", "Tails"]
    return pos[a]
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()
print("The first one is " + str(t1))
print("The first one is " + str(t2))
print("The first one is " + str(t3))

    
    #Write your code here
The first one is Tails
The first one is Tails
The first one is Heads

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

Homework

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

import random
num = random.randint(1,1000)
print(num)
print(bin(num)[2:])
print(hex(num))
# I converted it into hexidecimal as well
49
110001
0x31