''' Purpose: introduce some functions from module random ''' import random # want random values, then we need the random module # helps us generate random numbers or choose a random element from a sequence 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 to x and y 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 ) # if we pass a seed value, the random number generator is configured to # produce the same sequence of random values from that point on. # seed helps us control randomness; it provides a basis off of which future randomness is based # you do not need to set random.seed() equal to some variable to make it work # seed helps us influence whatever randomness is generated; if you want control, seed is the way to do it # it helps keep output consistent so that you can better see what could be going on in your program # it's also a great way of standardizing programs # See what happens when you run these lines! # Also a seed can be pretty much anything so long as it's legal Python. # random.seed('wubba lubba dub dub') # z = random.random() # generates a decimal between 0 and 1 # u = random.random() # print(z, u) # # random.seed('wubba lubba dub dub!') # d = random.random() pause.enter_when_ready() # let's make the seed random random.seed() #if you give nothing to seed it uses the time # so you don't see the same numbers over an over unlike a set seed bottom = 2 top = 7 step = 3 # function random.randrange( x ) returns a random integer from interval [ 0, x ); # that is, it generates a base x number # 0 to 6 is base 7 # 0 to 9 is base 10 # 0 to 1 is base 2 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 ) # from 2 to 6 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' ] # when things are in brackets [], the data type is a list # lists can contain so many things not just strings. # [ 2, '3', 2.789] this is a heterogeneous list. They are allowed in python. # most lists are homogeneous 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() pause.enter_when_ready() print( 'randrange(', bottom, ',', top, ') gave', x ) # bottom and top have to be ints 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 ) # ranging from x to y-1 in intervals of s x = random.randrange( bottom, top, step ) # from 2 to 6 in intervals of 2 is 2, 4, 6 # the order of the values in the parentheses makes a difference... # **try different numbers on your own and see what happens!** print( 'randrange(', bottom, ',', top, ',', step, ') gave', x ) # generates a number from 0 to 100 that is a multiple of 5 mult_of_5_from_0_to_100 = random.randrange(0, 101, 5) # random.randrange(starting_number, ending_number, interval) print('randrange(', '0', ',', '101', ',', '5', ') gave', mult_of_5_from_0_to_100) # how do I get a random number from x to y using randrange? # start your range from x and end it at y+1