''' Purpose: introduce some functions from module random ''' import random # want random values, then we need the random module print( 'By default, we can expect the random number generator to produce' ) print( 'a difference sequence of values every time we start it up' ) x = random.random() # random() returns a float number from float interval [0, 1 ) y = random.random() # because we did not set the seed, every time we run the program # should assign different values to the variables print( 'x:', x ) # x will be a different float every time we run the program print( 'y:', y ) # y will also be different every time print() ; input( 'Enter when ready: ' ); print() # if we set the seed value, the random number generator is configured to # produce the same sequence of random values from that point on. random.seed( 1112 ) # first number from seed( 1112 ) is always 0.28143470043335006. x = random.random() # how do we know this, we ran the #random.seed( 1112 ) # program and recorded the value y = random.random() #seed allows you to repeat the same random number # even though we are generating a different variable above, because we set the seed #as the same number in line 22, we will get the same number for y as we did for x # you can set the seed when using random.choice(): it will control the sequence #of choices that are made print( 'Because we set the seed to the same value before generating values for x' ) print( 'y, they are going to have the same value' ) print( 'x:', x ) print( 'y:', y ) print() ; input( 'Enter when ready: ' ); print() # let's make the seed random random.seed() #if you don't give seed an argument, it just randomizes behavior bottom = 2 top = 7 step = 2 # function random.randrange( x ) returns a random integer from interval [ 0, x ); # that is, it generates a base x number x = random.randrange( top ) #if you give a single value, it will generate random #integer from interval [ 0, x ) print( 'randrange(', top, '):', x ) print() ; input( 'Enter when ready: ' ); print() # function random.randrange( x, y ) returns a random integer from interval [ x, y ) x = random.randrange( bottom, top ) #can give it a range [ x, y ) print( 'randrange(', bottom, ',', top, '):', x ) print() ; input( 'Enter when ready: ' ); print() # function random.randrange( x, y, s ) returns random integer value from interval # [ x, y ); numbers are of form x + ( i * s ) x = random.randrange( bottom, top, step ) #step up by a certain value from range # [ bottom, top ) #step by default is 1 print( 'randrange(', bottom, ',', top, ',', step, '):', x ) print() ; input( 'Enter when ready: ' ); print() # function random.choice( s ) if s is a string, returns a random character from s keyboard = 'qwertyuiopasdfghjklzxcvbnm' letter = random.choice( keyboard ) #give choice a sequence, and it will pick #something from that sequence and hand it back print( 'choice( \'' + keyboard + '\' ):', letter ) # the \' means print an actual quote, don't take it as the start of a string print() ; input( 'Enter when ready: ' ); print() # function random.choice( s ) if s is a sequence, returns a random element from s days = [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ] day = random.choice( days ) print( 'choice(', days, '):', day ) print() ; input( 'Enter when ready: ' ); print() # random.shuffle( s ) if s is a sequence, randomly reorders s random.shuffle( days ) #give it a list, and it will mix up the order of the list print( 'Days is now:', days ) #it manipulates the original list: does NOT produce # a new list (you can modify lists, but not strings) print()