Lesson 3.14-3.15
Hacks
Unit 3 sections 14 and 15 Notes
- A software library contains procedures that can be used in the creation of new programs.
- Existing segments of code can come from internal or external sources, ie. libraries or previously written code.
- The use of libraries simplifies the task of creating complex programs.
- Application program interfaces (APIs) are specifications for how the procedures in a library behave and can be used.
-
Documentation for a library or API is necessary in understanding the key behaviors provided by the API/library and how to utilize them in your work.
-
A library is a collection of code from an external source that can be used to add functionality to a program.
- Libraries are very useful, as they can be used to save time and effort in the development process.
- Libraries are usually included in a program using a special keyword called " ." This keyword tells the program to look for the library and use its code.
Lesson 3.15.1
1) Randomization generates a value between two numbers. For example RANDOM(1,3) may result as 1 or 2 or 3, either one of those.
2) Now if we look into our day to day life we can see that randomization is all around us.
3) In order to use the random function we have to first import random at the very beginning.
Now lets look at the code below right here.
1) The lowest number that will be generated is 0 for answer 1 and for answer2, the lowest that will be generated is 1 so the lowest number that can be generated for answer 3 would be 1.
2) The highest number that will be generated to answer1 is 3 and the highest number that will be generated for answer 2 is 8 so the highest number for answer 3 would be 11.
3) The range of numbers that answer 3 could print out would be from 1 to 11.
import math
A = 16
print(math.sqrt(A))
#I imported the math library and it helped me be able to calculate the sqrt of A.
import math
A = 16
print(math.sqrt(A))
#I imported the math library and it helped me be able to calculate the sqrt of A.
-
Define what an import random function do
The import random loads the random module, which contains a number of random number generation-related functions
-
List a few other things that we can import other than random
-
TensorFlow.
- NumPy.
- SciPy.
- Pandas.
- Matplotlib.
- Keras.
- SciKit-Learn.
- PyTorch.
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
#imports the random library
spinner = random.randint(1,8)
#assigns the spinner variable the random generator
#if spinner <=3 then it will be green and so on for each of the other sections.
if spinner <= 3:
print("Green")
if spinner <= 5:
print("Blue")
if spinner <= 6:
print("Purple")
if spinner <= 7:
print("Red")
if spinner <= 8:
print("Orange")
- Also answer this question: What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?
The numbers that can be outputted from “RANDOM(12,20) are 12,13,14,15,16,17,18,19, or 20. Anything besides those numbers are excluded.
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)