import random # random is a library from pause import enter_when_ready # see http://www.cs.virginia.edu/~cs1112/documents/modules/random/ for a # discussion of interval notation # important one below is [ x, y ) -- this means all values starting with x # and up to but not including y # if we do not set the seed, the random number sequence differs with every run x = random.random() # random returns a float number from # float interval [0, 1) # because we did not set the seed, every time we run # the program the values assigned to x is different # running random.seed(x)...the seed is influenced by x print( "x = ", x ) 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 ) # seed is a way of helping us "control" or "influence" the randomness # random.seed('Simone') # the first value generated from this seed is 0.9538100080756253 # the randomness is based on the fact that computers represent numbers as binary values (0s and 1s) and a bunch of stuff about time y = random.random() # first number from seed( 1112 ) is always # 0.28143470043335006. how do we know this, we ran the # program and recorded the value print( "y = ", y ) z = random.random() # see what z is when you run this program with certain seeds...z's value isn't the same as y, # but z doesn't change value print( "z = ", z ) # random.seed( 1112 ) # # a = random.random() # see what a is when you run this program...it should be the same as x # print( "a = ", a ) # b = random.seed(1112) # print(b) # what happens when we run this? enter_when_ready() # rand.uniform( x, y ) returns a random float from the interval [ x. y ) n1_uniform = random.uniform( 1.5, 7.25 ) random.seed() # pick a new sequence of numbers based on the current time n2_uniform = random.uniform( 1.5, 7.25 ) print( "uniform values: ", n1_uniform, "and", n2_uniform ) enter_when_ready() # note: random.random() and random.uniform( 0, 1 ) are synonyms # rand.gauss( m, s ) returns a random normally distributed float # the generated value have mean m and standard deviation s n1_gauss = random.gauss( 1.5, 1.25 ) # mean = 1.5, std dev = 1.25 n2_gauss = random.gauss( 1.5, 1.25 ) print( "gaussian values:", n1_gauss, "and", n2_gauss ) enter_when_ready() bottom = 2 top = 7 step = 2 # 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 ) # we will get the value 0, 1, 2, 3, 4, 5, or 6 # random.randrange( x, y ) returns a random integer value from the interval # [ x, y ); values are equally likely to occur; print( "randrange( ",top," ) gave", x ) enter_when_ready() x = random.randrange( bottom, top ) # we will get the value 2, 3, 4, 5, or 6 print( "randrange( ", bottom, ",", top," ) gave", x ) enter_when_ready() # 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 ) # we will get the value 2, 4, or 6 enter_when_ready() # random.choice( s ) if s is a string, the method returns a random # character from the string alphabet = "qwertyuiopasdfghjklzxcvbnm" # a string is a collection...a list of characters letter = random.choice( alphabet ) # give random.choice() a collection, and it will hand you some random value from the collection print( "We like letter", letter ) print() # 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' ] # a list is a list 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 ) # if you give me a list, I'm going to reorder the elements print( "The ordering of days is now", days ) print() # feel free to play around with these random functions on your own time and learn more about these functions!