""" Purpose: introduce some another function from module random """ # by default the random module produces different interactions everytime we use it import random # whenever we want to use random, we have to import it and tell python where to find the functions random.seed( 1112 ) # setting a seed means you should get the same results every run # when you set the seed, you set it once before you start using choice() or randrange() # the seed can be an integer, float, string, etc. the seed can be anything # different data types will set the seed differently # random function choice( seq ) when a given a sequence as a parameter returns # a random element from the sequence ### get a string and choose four of its characters (repetition allowed) s = input( "Enter a string: " ) ch1 = random.choice( s ) # tells python to go to the random module, find the choice() function, and run it using s as the ch2 = random.choice( s ) # input ch3 = random.choice( s ) # without setting a seed, we have no clue or control over what it will choose ch4 = random.choice( s ) # the choice function chooses with replacement!! the same character can be pulled multiple times # runs of choice() are independent and not reliant on previous choice() runs. it is possible to get the same value # for ch1, ch2, ch3, and ch4, but it is not likely # ** choice() will work on any sequence, you could have a list of anything and choice() will work print () print( "Four random characters from string", s, ": ", ch1, ch2, ch3, ch4 ) print () ### get a list of words and choose four (repetition allowed) reply = input( "Enter some words: " ) user_words = reply.split() w1 = random.choice( user_words ) # same thing but looking at a list w2 = random.choice( user_words ) w3 = random.choice( user_words ) w4 = random.choice( user_words ) # could also do this in a for loop! # rand_words = [] # for i in range( 0, 4 ) : # w = random.choice( user_words ) # rand_words.append( rand_words ) # nested loop - generate an image of random noise # for r in range( 0, nrows ) : # for c in range( 0, ncols ): # pixel = random.choice( whatever ) # probably more stuff but that's the gist print( "Four random words from list", user_words, ":", w1, w2, w3, w4 ) # choice() without replacement - there likely is a function in the random module to choose without replacement, # or we could manually remove characters/words ourselves # on homeworks, you will be given the sequences to pick from and the value to set as the seed using random.seed() # with the seed, you will force the random generation to be non-random, allowing us to test your code # you will have expected program runs and you will be expected to produce the same output with the seed # if you don't use the seed() function, python will automatically use the current time to set as the seed # how does the seed() work? # random number generation comes from really weird kind of gross looking functions # you start with a seed value, run it through the function a set number of times, and you get something random # if we don't set the seed, it defaults to the time, meaning that each run is different # every data structure is stored somewhere in your computer as a whole bunch of 1s and 0s # "1112" is represented by a different string of 1s and 0s than 1112 is, so random.seed( '1112' ) and random.seed( 1112 ) # will produce a different set of random numbers, since they are different seeds