JK Week5 Random

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Junior Knights Week #5 Python Homework: Random Numbers

Problem A: Crazy Dice


Write a short python program that simulates rolling two twenty-sided dice, with labels 1 through
20. Your program should print out what shows on each die as well as their sum.

Sample Run
The first die shows 3.
The second die shows 14.
The total is 17.

Problem B: Many Crazy Dice


Write a program that simulates rolling two twenty-sided dice 50 times. Rather than printing out
each die roll, just print out the sum obtained from the two dice each time.

Sample Run
The dice sum to 28.
The dice sum to 13.
The dice sum to 39.
[Omitting 46 lines!!!]
The dice sum to 20.

Problem C: Snake Eyes on Crazy Dice


Write a program that simulates rolling two twenty-sided dice 100000 times, counting now many
times the sum of the two dice is 2. Based on these results as well as the results of the program
(snakeeyes.py) shown in class, make a guess as to how often one can expect to roll snake-eyes
(two 1s) on a pair of dice labeled 1 through n, for any positive integer n.

Sample Run
You got snake-eyes 35 times out of 100000.
That is .035 percent of the time.
Roughly, that is 1 in 2857 times.

Note: The numbers shown above are NOT realistic. This is just to show the format desired for
your output.
Problem D: Different Dice Game
Consider the following game for two players:

The first player rolls a pair of dice of fair six-sided dice. If the two dice show different numbers,
the player's score is the larger of the two numbers. Otherwise, the player's score is the sum of the
two numbers.

At this point, the player is asked if they want to remove their lower die and re-roll one die. If
they say no, the player keeps her score and their turn is over. If they say yes, then they roll one
more die and the number showing on this die replaces the lower of the original two rolls. Then
these two dice rolls are scored accordingly. (For example, if the initial roll was a 2 and 5, if the
player doesn't roll again, she gets 5 points. If she does roll again and obtains a 4, her score is still
5. If she rolled again and obtained a 6, her score would be a 6. If she rollwed again and obtained
a 5, her score would be a 10.)

The second player goes after the first, doing the exact same steps as the first player.

When the second player finishes his turn, the game ends. The winner is the person with the
higher score. If both players have the same score, they tie.

Write a program to simulate this game between two players. Print out the winner at the end (or
the fact that the players tied.)

Sample Run
Player 1, you rolled a 4 and a 3.
Do you want to reroll(yes/no)?
yes
Your second roll was a 5.
Your final score is 5.

Player 2, you rolled a 3 and a 3.


Do you want to reroll(yes/no)?
no
Your final score is 6.

Player 2, you win.


Sample Run
Player 1, you rolled a 2 and a 6.
Do you want to reroll(yes/no)?
no
Your final score is 6.

Player 2, you rolled a 2 and a 2.


Do you want to reroll(yes/no)?
yes
Your second roll was a 6.
Your final score is 6.

Both players tied.

Sample Run
Player 1, you rolled a 5 and a 4.
Do you want to reroll(yes/no)?
yes
Your second roll was a 5.
Your final score is 10.

Player 2, you rolled a 3 and a 4.


Do you want to reroll(yes/no)?
no
Your final score is 4.

Player 1, you win.

Problem E: Dice Game Looped


Write a program that allows two users to play the dice game in Problem D 10 times. At the
conclusion of the game, print out how many times player 1 won, how many times player 2 won,
and how many times both players tied.

You might also like