Section 3.14.1 - Libraries (Ethan Tran)

Learning Objective

To be able to select appropriate libraries and/or existing code to use in the creation of new programs.

College Board Essential Knowledge

  • 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.

Defining a Library

  • 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.

Example

import math

math.sqrt(64)
8.0

Review Questions (Answer for candy!)

1) In your own words, what is a library?

2) Why are libraries useful when writing a program?

3) What keyword is used to add a pre-made library?

Hacks

  • Write a program that uses a library/libraries in any sort of manner.
  • Explain your work/code



Lesson 3.15.1

What is randomization?

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.

Now for a piece of candy does anyone want to tell us an example of randomization during our day to day life.

3) In order to use the random function we have to first import random at the very beggining.

Now lets look at the code below right here.

import random

answer1 = random.randint(0,3)
answer2 = random.randint(1,8)
answer3 = answer1 + answer2
print(answer3)
8

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.

So a normal dice contains six sides with 1-6 on the individual sides. Which of the following code would simulate the result of rolling the dice three times and adds together all the obtained values together?
A.
import random
sum = random.randint(1,6) + random.randint(1,6) + random.randint(1,6)
print(sum)
12
B.
import random 
sum = random.randint(1,18)
print(sum)
17
C.
import random
sum = 3*random.randint(1,6)
print(sum)
12

1) Its not B because B is saying that it is going to choose one number between the range of 1 and 18 and not the sum of 3 diff numbers.

2) C is not correct also because C is saying that the computer is choosing one random between 1 and 6 and multiplying that 3 times but we want the sum of three different numbers between 1 and 6.

3) So the answer is A. .

For the last example:

import random
i = 1
while i < 4:
    choose = random.randint(1,10)
    print(choose)
    i += 1
6
9
8

Which answers could not be possible for the code above?

A. 4 7 6

B. 9 1 8

C. 2 11 5

D. 5 10 2

Random can also have different methods, in the above examples we only used one example, randint which returns a random number between the given range. Down below I have created a table of all the methods with the description of what it is used for with the random function.

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)

Lesson Recap: 1) random function generates a random number from a to b (range of two numbers)

2) You need to import random in order to use the random function

3) Every result from an import function is equally likely to occur

Hacks for lesson 3.15.1

  • Write a few lines of code that implements the import function

  • Define what an import random function do

  • List a few other things that we can import other than random



3.15.2 Lesson

  • Some Essential Knowledge that you need to know
  • RANDOM (a,b) will provide you with a random integer between the numbers a-b
  • Ex. RANDOM (7,18) can provide you with the number 13.
  • Using a random generator means each result can come out as different.

Lets Try an Example Scenario

  • Try and formulate a way to write a segment of code that can simulate the flipping of a coin.
  • You have to use a random generator and assign the output of "Heads" or "Tails" to a specified result.
import random
flip = random.randint(1,2)
 
if flip == 1:
    print("Heads")
else:
    print("Tails")

# Try this code for yourself!
  • This is a pretty simple code to understand.
  • First we use the randomization here between the 1 and 2 to determine which side of the coin we get.
  • If we get 1 then we get heads. If we get 2 or anything else than 2, then we get Tails.

Now let's answer questions using your knowledge.

  • If you want yes to show up 50% of the time, which of the code segments can you use?

random(1,100) <= 50 random(1,100) == 50 random(1,10) <= 5 random(1,10) >= 5

  • The answer is A,C, and D!

Why isn't the answer B?

  • This because when you are saying equal to 50, you are saying only when this one number shows up the output will be yes.
  • You want it to come up 50 percent of the time not just when that one number shows up.

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.

  • Also answer this question: What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?

Hacks Summary

Hacks 3.14.1

  • Write a program that uses a library/libraries in any sort of manner.
  • Explain your work/code

Hacks 3.15.1

  • Write a few lines of code that implements the import function

  • Define what an import random function do

  • List a few other things that we can import other than random

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.

  • Also answer this question: What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?