''' Purpose: introduce some functions from module random ''' import random # want random values, then we need the random module # random module helps us generate random numbers or choose a random element from some sequence import pause # you need pause.py in order to run this program; from a previous class 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 # including 0 and excluding 1 # inherent bias to random number generators? - yes! because pseudo-random # Python's random number generator uses the time as a seed print( 'x =', x ) print( 'y =', y ) pause.enter_when_ready() # need to import pause in order for this line to run # 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 random.seed( 1112 )# first number from seed( 1112 ) is always 0.28143470043335006. how do we know this, we ran the program and recorded the value # for loop! - gives us the ability to repeat work for i in range(0, 1000): # this range signifies how many times we want the action repeated # random.seed(1112) # where you put lines of code matters...ESPECIALLY with loops!!! # x = random.random() # if you run this for loop multiple times, with a seed, you should get the same output x = random.randrange(0, 101, 5) print('At iteration', i, 'x is', x) # why are there a lot of functions that exclude the last number? # because that's the standard and because it is just barely faster # everything else starts at 0 and ends at some n-1, so might as well do that for everything # random.seed( 1112 ) # resetting the seed makes the next value the first value generated from that seed # y = random.random() # see what happens when you comment out the previous line...play with Python on ya own! # 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() 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() # not giving the seed a value = setting a random seed...based on the time 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 # interval including 0 and excluding x (from 0 to x-1) x = random.randrange( top ) # gives us values between 0 and 6 print( 'randrange(', top, ') gave', x ) # different time -> different seed -> different outcome # 0 to 6 is base 7 # 0 to 9 is base 10 # 0 to 1 is base 2 # fun number facts! :D 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 # including (inclusive of) x and excluding (exclusive of) y...from x to y-1 x = random.randrange( bottom, top ) # from 2 to 6 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 ) # c = random.randrange(3, 12, 5.5) # starting from 3, not including 12, in intervals of 5.5 # 3, 8 (not 13 because 13 > 12) # print('randrange(', '3', ',', '12', ',', '5.5', ') gave', c) # should output an error # nothing is really RANDOM because there is a basis for it when we program, but it's as close to random as we can get # 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 # **what happens when I do this? - do it yourself and tell us!** # how to do randrange with decimals? - it's pretty much just getting creative with various functions and values 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()