Lab 13: Pseudo-random Generation
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Lab 13: Pseudo-random Generation
The purpose of this lab is to give you practice working with some of the func- tions in Python’s random library.
Grab a copy of lab13 .py from Blackboard: you will need to run this file, and then make some additions to it.
Part 1: Observing Random Numbers
Load the script lab13 .py in the Python shell.
1. Invoke the function call diceExperiment (). Record in the box below the first ten numbers that are printed (i.e., the line immediately after the The dice rolls generated: line):
2. Invoke the function call diceExperiment () a second time. Record in the box below the first ten numbers that are printed:
3. What observation can you make, as you compare the results from these two different runs?
4. Replace the seed (54321) line with seed(1234567). Reload the script, and invoke the function call diceExperiment () yet again. Record in the box below the first ten numbers that are printed:
5. Invoke the function call diceExperiment () again. Record in the box below the first ten numbers that are printed:
6. What observation can you make, as you compare the results from these two runs to those of the first two runs?
7. Now comment out the seed(1234567) line. Reload the script, and in- voke the function call diceExperiment () yet again. Record in the box below the first ten numbers that are printed:
8. Invoke the function call diceExperiment () one final time. Record in the box below the first ten numbers that are printed:
9. What observation can you make, as you compare the results from the fi- nal two different runs? How can these results be explained by the current version of the code?
Part 2: A Guessing Game
For this part, you will need to complete the function definition guess
(low,hi) to simulate a little guessing game:
1. The computer selects a pseudo random integer between low and hi. (Note: both low and hi can be selected, as well as any integers that lie strictly between them. All eligible numbers should be equally possible.)
2. Until the user has successfully guessed the number, the computer repeat- edly does the following:
● Prompts the user to guess a number (an integer)
● Reads in that integer
● If the user’s guess is less than the computer’s number, the computer tells the user ”Go higher” .
● If the user’s guess is greater than the computer’s number, the com- puter tells the user ”Go lower” .
3. Once the user has successfully guessed the number, the computer tells the user how many guesses it took.
Here’s a sample run of the function:
>>> guess (1,10)
I’m thinking of a number between 1 and 10 (inclusive) .
What’s your guess? 5
Go lower
What’s your guess? 3
Go lower
What’s your guess? 1
You guessed it in 3 guesses
>>> guess (1,10)
I’m thinking of a number between 1 and 10 (inclusive) . What’s your guess? 5
Go higher
What’s your guess? 7
Go higher
What’s your guess? 9
Go lower
What’s your guess? 8
You guessed it in 4 guesses
>>> guess (20,98)
I’m thinking of a number between 20 and 98 (inclusive) .
What’s your guess? 50
Go lower
What’s your guess? 35
You guessed it in 2 guesses
Important constraints: Your function should conform to the following con- straints:
● ●
● ● |
Do not use break or seed in your function. Do not change/delete any of the existing code in the function definition. Instead, simply make your additions. Everything you add should be inside the definition of the function guess. You will need three distinct variables, to represent the following items: the computer-generated random number, the user’s current guess, and the to- tal number of guesses the user’s taken (codified in the variable guesses that has already been included). |
What and How to Submit
Submit your modified lab13 .py to Blackboard (along with this writeup, with your answers filled in). Do not forget to include Shell test runs.
2023-04-12