I need to create a program similar to the Monty Hall game \'Lets make a Deal\' i
ID: 3880748 • Letter: I
Question
I need to create a program similar to the Monty Hall game 'Lets make a Deal' in Python using random number generator. what i don't understand is the following part of the assignment: ' The seed command can be used to control the sequence of random numbers generated. This will fix the sequence of execution for your program. The following code will always print the same output.
import random
random . s e e d ( 1 0 )
p r i n t ( random . r a n di n t ( 0 , 1 0 ) )
The below code will be random.
import random
random . s e e d ( )
p r i n t ( random . r a n di n t ( 0 , 1 0 ) )
We recommend you use a seed when developing your code, when switch to random values once you have tested it. You should only call seed once for your entire program.'
We are supposed to create a list with the car behind door 1 then shuffle a list of doors to randomize the car location. We then have the player pick a door from 1-3 and then monty picks a door with a goat then determine whether the player should switch or not. Please do not give me the full code but explain how I can go about doing this because at the moment I am at a loss.
Explanation / Answer
Ok.. so basically you want to understand the effect of seed in random function..
Random function is actually used to create random numbers which are not in order.. but random function also uses some algorithm inside it which generates numbers in a particular sequence, which are relatively random..
So if a random function genertes a value 'x', then the next value is computed using the algorithm on top of this value 'x'. Hence if after 'x', 'y' is the next value.. then no matter how many times you run the program.. if you get a value as 'x', then next value for sure be 'y'.
So basically values are generated on the basis of previously generated value. now for the first time execution of the program, previously generated value is null, hence, program picks a random number as the previous value(mostly it is system time in nanoseconds, which will keep changing with each iteration of your execution) and on basis of that it generates a new value.. this previous value is known as seed.
Now if we mention the seed as '100'(suppose), for every execution, the next value will be computed on basis of '100' only.. so you are asked to put a seed in your code, so that each time you run your program.. your program gives the same sequence of numbers, which are random relative to each other...
Below piece of code will help you understand it bit more..
====================
import random
random.seed( 100 )
print("Random number with seed 100 : ", random.random()) #will generate a random number, lets say 0.1456692551041303
#now if we use the same random number once again in program, we will again get the same number from random function.. that is 0.1456692551041303
random.seed( 100 )
random.random() # same random number as before, as 0.1456692551041303
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.