More on The Random Module
Write a program, random_module_YI.py with this short program to understand how random.randrange works.
# Show the randrange range with the "while" loop import random i = 0 counter1 = 0 counter8 = 0 print("This program is silently counting the number of times") print("the numbers 1 and 8 randomly come up with the following ") print("instruction: randNumber = random.randrange(1,8)") while i < 1000: i += 1 randNumber = random.randrange(1,8) if randNumber == 1: counter1 += 1 if randNumber == 8: counter8 += 1 print("\nThe number 1 showed up ", counter1, " number of times") print("The number 8 showed up ", counter8, " number of times") >>> This program is silently counting the number of times the numbers 1 and 8 randomly come up with the following instruction: randNumber = random.randrange(1,8) The number 1 showed up 141 number of times The number 8 showed up 0 number of times
Answer these questions on the corresponding post:
1. What is needed to import to be able to use the randrange function?
2. What does the body of the loop do?
3. What are the two “arguments” used in randrange?
3. What can you say about the second “argument” in randrange
Assignment:
Write a program, one_die_YI.py that prompts the user how many times to roll a six-sided die and a number on the die. Then it displays how many times the numbers come up.
The output should look like this:
>>> User friendly introduction... How many times do you want to roll a six-sided die? 500 What number would you like to know how many times it comes up? 3 The number 3 showed up 61 number of times in 500 of rolls User friendly exit ... >>>
Assignment:
Write a program, one_die_all_YI.py that prompts the user how many times to roll a six-sided die. Then it displays how many times all the numbers come up.
How many times should a die be rolled to validate the theoretical probability for each face to come up?