''' Purpose: introduce some functions from module random ''' import random # want random values, then we need the random module import pause 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 assigned to print( 'x:', x ) print( 'y:', y ) pause.enter_when_ready() # 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() 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 ) pause.enter_when_ready() # let's make the seed random random.seed() 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 ) print( 'randrange(', top, '):', x ) pause.enter_when_ready() # function random.randrange( x, y ) returns a random integer from interval [ x, y ) x = random.randrange( bottom, top ) print( 'randrange(', bottom, ',', top, '):', x ) pause.enter_when_ready() # 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 ) print( 'randrange(', bottom, ',', top, ',', step, '):', x ) pause.enter_when_ready() # function random.choice( s ) if s is a string, returns a random character from s keyboard = 'qwertyuiopasdfghjklzxcvbnm' letter = random.choice( keyboard ) print( 'choice( \'' + keyboard + '\' ):', letter ) pause.enter_when_ready() # 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 ) pause.enter_when_ready() # random.shuffle( s ) if s is a sequence, randomly reorders s random.shuffle( days ) print( 'Days is now:', days ) print()