Question
angle = randrange(0,360)
tom.right(angle)
tom.forward(10)
2. Is it likely that a monkey at a typewriter, randomly choosing character, could write Hamlet?
Write a simulation to answer this question. That is, write a program that will randomly generate 100,000 strings of length 5 and count the number of strings that match "to be". You may assume that your strings are lower case letters and spaces only. That is, when you generate your string, you are choosing from the 27 characters:
[' ','a','b','c','d',...'y','z']
3. Write a simulation of the rolling of 2 six-sided dice. Your program should have a function that oneRoll() that returns the sum of rolling your dice. You may assume that each of the six sides is equally likely to be rolled (that is, the dice are "fair"). Run your simulation 10,000 times and report the frequency that each sum occurred.
A sample run of your program should look something like (but not identical due to the randomness of the simulation):
2 : 292
3 : 536
4 : 810
5 : 1100
6 : 1428
7 : 1631
8 : 1439
9 : 1100
10 : 825
11 : 543
12 : 296
4. Write a simulation of the rolling of 2 dice: 1 four-sided die and 1 eight-sided die. That is, the first die has four sides, and you can roll, with equal probability: 1,2,3, or 4. The second die has eight sides, and you can roll with equal probability: 1, 2, 3, 4, 5, 6, 7, or 8. Your program should have a function that oneRoll() that returns the sum of rolling your dice. Run your simulation 10,000 times and report the frequency that each sum occurred.
A sample run of your program should look something like (but not identical due to the randomness of the simulation):
2 : 308
3 : 629
4 : 932
5 : 1231
6 : 1230
7 : 1242
8 : 1343
9 : 1198
10 : 938
11 : 617
12 : 332
Solution Preview
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.
mport randomdef oneRoll():
die1 = random.randrange(1,7)
die2 = random.randrange(1,7)
return die1+die2
count = [0]*11
for i in range(10000):
result = oneRoll()
if result == 2:
count[0] = count[0]+1
elif result == 3:
count[1] = count[1]+1
elif result == 4:
count[2] = count[2]+1...
By purchasing this solution you'll be able to access the following files:
Solution.zip.