Lab 1: Pre-lab¶
Refresh on Python¶
-
Fibonacci sequence is a series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
The first two numbers are 0 and 1. The rest of the numbers in the sequence is found by summing up two numbers before it.
-
Create a function, in Python, which takes the sequence length sl as the input of the function and prints the Fibonacci sequence of the length sl.
1 2 3
def fibonacci(sl): ... return fib_seq
Random selection based on probability¶
For this section. assume the random.random() function selects the random number with even probability.
-
Consider a coin tossing event. If the probabilities of getting a head or a tail are even, i.e. 50%. Create a Python function which will simulate the coin tossing event and return the result as
headortail.1 2 3
def tossCoin(): ... return headOrTail -
If the probabilities of getting a head or a tail are not even, with head as 20% and tail as 80%, how would you change the Python function you created previously to adapt to this coin?
-
Consider the event of selecting one option out of three options randomly. The probability of choosing option
Ais 20%,Bis 50%, andCis 30%. Create a Python funciton to simulate the random selection of the options.1 2 3
def chooseFromThree(): ... return selectedOption