Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The random module can be used to generate random ints in a specified range. Note that randint(n1,n2) generates random ints n for n1 >> import

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

The random module can be used to generate random ints in a specified range. Note that randint(n1,n2) generates random ints n for n1 >> import random >>> for i in range (10): print (random.randint (1,100)) 61 89 23 13 80 56 93 65 22 77 Suppose you wanted to use the random module to roll a fair six-sided dice 20 times and generate a list of the 20 dice rolls. You could use what is known as list comprehension: >>> [random.randint (1,6) for i in range (20)] [4, 5, 4, 3, 6, 6, 6, 4, 6, 4, 3, 6, 3, 1, 4, 1, 4, 4, 2, 5] >>> [random.randint (1,6) for i in range (20) ] [1, 2, 1, 6, 5, 4, 4, 5, 1, 4, 5, 2, 2, 1, 4, 1, 1, 5, 5, 2] >>> Note that this is done twice above and the same result is not obtained the second time. Make sure you understand the difference between events that are mutually exclusive (events that can't occur simultaneously) and non-mutually exclusive events (events that can occur simultaneously). You use if(cond 1): ..... elif(cond2); for the former and instead use if(cond1): ....... If(cond2) for the latter, where cond1 is True for event 1 and cond2 is True for event 2. 1. (10 pts] Use list comprehension to write a function, RollFour(), that returns a list of a roll of a fair dice 4 times. Use a for loop to execute a four-roll 8 times, printing out the result. 2. [10 pts] Use your function from Q1 to roll a dice 4 times, then use only one for loop to repeat this 100 times, and count the number of times the sum of the 4 dice rolls is greater than 20 and the number of times the sum of the 4 dice rolls is less than or equal to 15. Print out each count using a labelled print. Your code should reflect whether the above two conditions are mutually exclusive or not. Hint: a counter is just a special sum accumulator that is incremented by 1 if a certain condition is met. You can use a sum accumulator to sum the list, but you can also use the sum function on a list of number to sum up the list: >>> L = [2,4,5) >>> sum (L) 11 When you write your code, make sure that, for each of the 100 4-rolls, you make the 4-roll only once. That is important - why? 3. [10 pts] Use your code from Q1 to roll a dice 4 times, then use only one for loop to repeat this 100 times, and count the number of times the sum of the 4 dice rolls is greater than 20 and the number of times the sum of the 4 dice rolls is greater than 21. Your code should reflect whether the above two conditions are mutually exclusive or not. Print out each count using a labelled print. What can you say with certainty about the count for sums greater than 20 and the count for sums greater than 21? Explain. 4. [10 pts] Compare & contract the code for Q2 and for Q3. Put this into a triple quote string Or you can comment it out. The random module can be used to generate random ints in a specified range. Note that randint(n1,n2) generates random ints n for n1 >> import random >>> for i in range (10): print (random.randint (1,100)) 61 89 23 13 80 56 93 65 22 77 Suppose you wanted to use the random module to roll a fair six-sided dice 20 times and generate a list of the 20 dice rolls. You could use what is known as list comprehension: >>> [random.randint (1,6) for i in range (20)] [4, 5, 4, 3, 6, 6, 6, 4, 6, 4, 3, 6, 3, 1, 4, 1, 4, 4, 2, 5] >>> [random.randint (1,6) for i in range (20) ] [1, 2, 1, 6, 5, 4, 4, 5, 1, 4, 5, 2, 2, 1, 4, 1, 1, 5, 5, 2] >>> Note that this is done twice above and the same result is not obtained the second time. Make sure you understand the difference between events that are mutually exclusive (events that can't occur simultaneously) and non-mutually exclusive events (events that can occur simultaneously). You use if(cond 1): ..... elif(cond2); for the former and instead use if(cond1): ....... If(cond2) for the latter, where cond1 is True for event 1 and cond2 is True for event 2. 1. (10 pts] Use list comprehension to write a function, RollFour(), that returns a list of a roll of a fair dice 4 times. Use a for loop to execute a four-roll 8 times, printing out the result. 2. [10 pts] Use your function from Q1 to roll a dice 4 times, then use only one for loop to repeat this 100 times, and count the number of times the sum of the 4 dice rolls is greater than 20 and the number of times the sum of the 4 dice rolls is less than or equal to 15. Print out each count using a labelled print. Your code should reflect whether the above two conditions are mutually exclusive or not. Hint: a counter is just a special sum accumulator that is incremented by 1 if a certain condition is met. You can use a sum accumulator to sum the list, but you can also use the sum function on a list of number to sum up the list: >>> L = [2,4,5) >>> sum (L) 11 When you write your code, make sure that, for each of the 100 4-rolls, you make the 4-roll only once. That is important - why? 3. [10 pts] Use your code from Q1 to roll a dice 4 times, then use only one for loop to repeat this 100 times, and count the number of times the sum of the 4 dice rolls is greater than 20 and the number of times the sum of the 4 dice rolls is greater than 21. Your code should reflect whether the above two conditions are mutually exclusive or not. Print out each count using a labelled print. What can you say with certainty about the count for sums greater than 20 and the count for sums greater than 21? Explain. 4. [10 pts] Compare & contract the code for Q2 and for Q3. Put this into a triple quote string Or you can comment it out

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Semantics In Databases Second International Workshop Dagstuhl Castle Germany January 2001 Revised Papers Lncs 2582

Authors: Leopoldo Bertossi ,Gyula O.H. Katona ,Klaus-Dieter Schewe ,Bernhard Thalheim

2003rd Edition

3540009574, 978-3540009573

More Books

Students also viewed these Databases questions