Unit 3.15 & 3.15 Notes and Hacks
Notes
Key Terms
- Software Library: Contains procedures that can be used in the creation of new programs
- Randomization generates a random value between any two numbers. ### 3.14
- Existing segments of code can come from both internal and external sources.
- The use of libraries simplifies the task of creating complex programs.
- Documentation for a library is necessary in understanding key behaviors
- You can import those libraries such as math. ### 3.15
- You need to import random to allow a random number generator.
- Randomizing will choose a random value between the specified domains.
- There are many different commands for different imports
import math
def cos(a):
a = int(input())
result = math.sqrt(a)
return result
import random
rndm = random.randint(1,2)
if rndm == 1:
print("heads")
if rndm == 2:
print("tails")
- Define what an import random function do
- A random function can choose a number between two different numerical values. An example is random(1,8) will choose a random number between 1 and 8 inclusive. This helps to simplify code as writing a random number generator would use up a lot more space than "import random"
- List a few other things that we can import other than random
- Besides random and math, you can also import Seaborn which is useful for plotting graphs and creating tables.
Hacks 3.15.2
- For your hacks you need to create a random number generator that will simulate this situation:
- There is a spinner divided into eight equal parts. 3 parts of the spinner are green, two parts are blue, one part is purple, one part is red, and one part is orange. How can you simulate this situation using a random number generator.
import random
rndm = random.randint(1,8)
if 3 >= rndm:
print("green")
if rndm == 4:
print("blue")
if rndm == 5:
print("blue")
if rndm == 6:
print("purple")
if rndm == 7:
print("red")
if rndm == 8:
print("orange")
- Also answer this question: What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?
- All of the possible numbers printed are al integers from 12 to 20 inclusive while anything that is not between 12 and 20 would be excluded.