Lesson 3.14.1

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 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)

For 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

Lesson 3.15.2