''' Purpose: introduce some functions from module random ''' import random # want random values, then we need the random module import pause print( 'Because we have not the seed before generating values for x and ' ) print( 'y, we can expect the first two values to be randon every run' ) 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 pass a 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 x = random.random() # 0.28143470043335006. 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 randon random.seed() bottom = 2 top = 7 step = 2 # function random.randrange( x ) # returns a random integer value from the interval [ 0, x ); values are # equally likely to occur. we say that the generated numbers are from base x x = random.randrange( top ) print( 'randrange(', top, ') gave', x ) pause.enter_when_ready() # function random.randrange( x, y ) # returns a random integer value from the interval [ x, y ); values are # equally likely to occur x = random.randrange( bottom, top ) print( 'randrange(', bottom, ',', top, ') gave', x ) pause.enter_when_ready() # function random.randrange( x, y, s ) returns a random integer value from the interval # [ x, y ); values are equally likely to occur; generated numbers are of form # x + ( i * s ) x = random.randrange( bottom, top, step ) print( 'randrange(', bottom, ',', top, ',', step, ') gave', x ) pause.enter_when_ready() # function random.choice( s ) if s is a string, the method returns a random # character from the string; the characters are equally likely to occur alphabet = 'qwertyuiopasdfghjklzxcvbnm' letter = random.choice( alphabet ) print( 'We like letter', letter ) print() # function random.choice( s ) if s is a sequence, the method returns a random # element from the sequence days = [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ] appointment = random.choice( days ) print( 'We will next see you on', appointment ) print() # random.shuffle( s ) if s is a sequence, randomizes the ordering of # the values in the sequence random.shuffle( days ) print( 'The ordering of days is now', days ) print()