Unit 3.15 Random Values Student Copy
Here is our lesson about random values! yay!
- What are Random Values?
- Why do we need Random Values for code?
- Random values can be used in coding:
- Challenge #1
- Homework
- Libraries
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:
import random to be able to use the feature (its a package) random.randint(number,number) to make a range of numbers it can pick a random item from a list because it assigns a number to each item on the list
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 -dice -coin flipping
import random
random_number = random.randint(1,100)
print(random_number)
import random
def randomlist():
list = ["apple", "banana", "cherry", "blueberry"]
element = random.choice(list)
print(element)
randomlist()
Real Life Examples: Dice Roll
import random
for i in range(3):
roll = random.randint(1,6)
print("Roll " + str(i + 1) + ":" + str(roll))
def coinflip():
thecoins = ["heads", "tails"]
randomsolution = random.randint(0,1)
print(thecoins[randomsolution])
coinflip()
EXTRA: Create a function that will randomly select 5 playing Cards and check if the 5 cards are a Royal Flush
import random
def decToBinary(n):
binaryNum = [0] * n;
i = 0;
while (n > 0):
binaryNum[i] = n % 2;
n = int(n / 2);
i += 1;
for j in range(i - 1, -1, -1):
print(binaryNum[j], end = "");
n = random.randint(0,200);
print(n)
decToBinary(n);
from datetime import date
import random
import math
days_dictionary = {
1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31,
}
day = int(input("give a day!"))
month = int(input("give a month!"))
year = int(input("give a year!"))
thedate = str(month) + "-" + str(day) + "-" + str(year)
print(thedate + " is the date you chose")
randommonth = random.randint(1,12)
RDO = days_dictionary.get(randommonth)
randomday = random.randint(0,RDO)
USD = str(randommonth) + "-" + str(randomday) + "-" + str(year)
print(USD + " is the randomized date")
def difdays(month, day):
dif = 0
for i in range(1,month):
dif += (days_dictionary.get(i))
dif += day
return(dif)
givendays = difdays(month, day)
randomdays = difdays(randommonth, randomday)
x = abs(randomdays-givendays)
print("the difference between these two dates is " + str(x) + " days")
Libraries
Okay, so we've learned a lot of code, and all of you now can boast that you can code at least some basic programs in python. But, what about more advanced stuff? What if there's a more advanced program you don't know how to make? Do you need to make it yourself? Well, not always.
You've already learned about FUNCTIONS that you can write to reuse in your code in previous lessons. But,there are many others who code in python just like you. So why would you do again what someone has already done, and is available for any python user?
PACKAGES allow a python user to import METHODS from a LIBRARY, and use the methods in their code. Most libraries come with DOCUMENTATION on the different methods they entail and how to use them, and they can be found with a quick GOOGLE SEARCH. methods are used with the following:
Note: a method from a package can only be used after the import statement.
Some libraries are always installed, such as those with the LIST methods which we have previously discussed. But others require a special python keyword called IMPORT. We will learn different ways to IMPORT in Challenge 1.
Sometimes we only need to import a single METHOD from the package. We can do that with the word FROM, followed by the package name, then the word IMPORT, then the method. This will alllow you to use the method without mentioning the package's name, unlike what we did before, however other methods from that package cannot be used. To get the best of both worlds you can use "*"
To import a method as an easier name, just do what we did first, add the word AS, and write the name you would like to use that package as.
import random as rd
print(rd.randint(0,5))
import math
x = math.isclose(2,10)
print(x)
import math
from math import *
a = float(input("give a number!"))
b = float(input("give a second number!"))
print("when you round up a you get" + " " + str(ceil(a)))
print("when you round up b you get" + " " + str(ceil(b)))
print("when you round down a you get" + " " + str(floor(a)))
print("when you round down b you get" + " " + str(floor(b)))
print("the least common multiple of a and b rounded down is..." + " " + str(lcm(floor(a), floor(b))))
print("the greatest common denominator of a and b rounded up is..." + " " + str(gcd(ceil(a), ceil(b))))
print("the factorial of a is " + str(factorial(floor(a))))
print("the factorial of b is " + str(factorial(floor(b))))
print("are a and b close? (within 0.9) " + str(isclose(floor(a), floor(b))))