""" Purpose: introduce some another function from module random """ # by default the random module produces different interactions everytime we use it import random # random is another python library that must be imported # without providing a seed, the seed to set the random machine depends on the current time # and time is always increasing, so the random machine will be reset every new time # seed() is a function in random # pass in ANYTHING --> it will set the random machine into a certain state # the seed can be a number (ints, floats), string, lists,... # so then every time you run the program, it will choose the same randomness regardless of the run # of the program and regardless of your computer # if you pass nothing into the function, the seed will be the current time, so will be random every time you run # the program (the same as not using the seed() function at all) random.seed('1112') # '1112' is a different seed than number 1112 --> produce different random choices # you don't need to know how a random machine works - it's just a black box --> just know it produces # random things and choices # 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: " ) # random.choice( s ) --> function in random where you can give it a string, it gives you a random character # from that string # this will be random choice every time you run it, and different on each computer # each use of the choice function will produce a random choice for a character within the string s # two random choices may be the same i1 = random.choice( s ) i2 = random.choice( s ) i3 = random.choice( s ) i4 = random.choice( s ) # if seed is set to random.seed('1112') # choice will choose a random string but it will be the same choice everytime you run the program # let s = 'abcdefghij' # so i1 may choose 'j' from s, i2 may choose 'f' # and these choices will be the same even if you rerun the program # if seed is not set, then when you rerun the program, i1, i2, i3 and so on will have different choices print () print( "Four random characters from string", s, ": ", i1, i2, i3, i4 ) print () ### get a list of words and choose four (repetition allowed) reply = input( "Enter some words: " ) user_words = reply.split() # can also pass in a list into random.choice( list ) and it will hand back a random element from the list w1 = random.choice( user_words ) w2 = random.choice( user_words ) w3 = random.choice( user_words ) w4 = random.choice( user_words ) print( "Four random words from list", user_words, ":", w1, w2, w3, w4 )