""" Demonstrates how to generate random (actually, PSEUDO-random) numbers. Authors: David Mutchler, Vibha Alangar, Dave Fisher, Matt Boutell, Mark Hays, Mohammed Noureddine, Sana Ebrahimi, Sriram Mohan, their colleagues and PUT_YOUR_NAME_HERE. """ ############################################################################### # The random module generates pseudo-random numbers. # It ships with Python, hence is convenient, but does NOT generate # pseudo-random numbers that are secure enough for cryptographic purposes. ############################################################################### import random import time # random.seed() # Same as not setting the seed # random.seed(392) # 392 was picked arbitrarily t = time.time() # The number of seconds since January 1, 1970, as a float random.seed((t * 10000) % 10000) # The first 4 decimals of t def main(): """ Generate and print 10 random numbers between 1 and 100, inclusive. """ for k in range(10): r = random.randrange(1, 101) # Random between 1 and 100, inclusive print("{:3}".format(r)) ############################################################################### # NOTES ON THE ABOVE: # # Pseudo-random numbers are generated by: # 1. Start with a "seed" number. Let's call it r1. # 2. Do some "scrambling" operations on R, e.g.: # -- Multiply r1 by (say) 9839471, represent the result in binary, # take the middle (say) 20 bits, # and repeat that process (many times) on those middle 20 bits. # -- Real "scrambling" algorithms are much more sophisticated than # the above simple-minded example. # to get the next pseudo-random number r2. # 3. Repeat step 2, but using r2 to generate r3, and so forth. # # You can set the "seed" to any of the following: # a. A specific (but arbitrary) number. That causes the same sequence # of pseudo-random numbers each time the program runs. # b. Nothing, in which case the seed is set in a manner that depends on # the operating system (Windows, Linux, Mac, etc). # c. A number based on the current time, which is more or less random. # # random.randrange(m, n) returns a random integer from m to n-1, inclusive. # # There are lots of other useful functions in the random library # if you need more sophisticated randomness. ############################################################################### # ----------------------------------------------------------------------------- # Calls main to start the ball rolling. # ----------------------------------------------------------------------------- main()