Unit 3 Lesson 14 - 15 Hacks and Notes
Libraries and Random Values
Notes:
- A library is a collection of code from an external source that can be used to add functionality to a program.
- Randomization generates a value between two numbers. For example RANDOM(1,3) may result as 1 or 2 or 3, either one of those.
-
Method | Description
seed() | Initialize the random number generator
getstate() | Returns the current internal state of the random number generator
setstate() | Restores the internal state of the random number generator
getrandbits() | Returns a number representing the random bits
randrange() | Returns a random number between the given range
randint() | Returns a random number between the given range
choice() | Returns a random element from the given sequence
choices() | Returns a list with a random selection from the given sequence
shuffle() | Takes a sequence and returns the sequence in a random order
sample() | Returns a given sample of a sequence
random() | Returns a random float number between 0 and 1
uniform() | Returns a random float number between two given parameters
betavariate() | Returns a random float number between 0 and 1 based on the Beta distribution (used in statistics)
expovariate() | Returns a random float number based on the Exponential distribution (used in statistics)
gammavariate() | Returns a random float number based on the Gamma distribution (used in statistics)
gauss() | Returns a random float number based on the Gaussian distribution (used in probability theories)
lognormvariate() | Returns a random float number based on a log-normal distribution (used in probability theories)
normalvariate() | Returns a random float number based on the normal distribution (used in probability theories)
vonmisesvariate() | Returns a random float number based on the von Mises distribution (used in directional statistics)
paretovariate() | Returns a random float number based on the Pareto distribution (used in probability theories)
weibullvariate() | Returns a random float number based on the Weibull distribution (used in statistics)
import math
number = input("Insert the number that you want to find the greatest common divisor of")
def gcdfunction(number):
result = math.gcd(int(number))
return result
print("The greatest common divisor is: " + str(gcdfunction(number)))
First the library math is used and imported to ensure that the math functions are able to be used. Then the variable number is set where a person inputs the number that they wish to take the greatest common divisor of. A function is then defined with a result variable that is set equal to the greatest common divisor of the integer that the user input. The result is then returned and printed in the sentence stating what the greatest common divisor.
import random
spinner_lands = random.randint(1, 8)
if spinner_lands <= 3:
color = "green"
elif spinner_lands <= 5:
color = "blue"
elif spinner_lands <= 6:
color = "purple"
elif spinner_lands <= 7:
color = "red"
elif spinner_lands <= 8:
color = "orange"
print("The color you landed on is: ", color)
- What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?
The numbers that can be outputted are any number from 12 to 20 including 12 and 20.