# File: random.py # # This prints random numbers, strings, and lists in various ways # # Matt Bishop, MHI 289I, Fall 2020 # import random # print 5 random numbers print("Some random numbers between 0 and 1") for i in range(5): print("Random number #{0:d}".format(i+1), "is", random.random()) # wait for user prompt input("Return to continue") # print 5 more random numbers print("\nSome more random numbers between 0 and 1") for i in range(5): print("Random number #{0:d}".format(i+1), "is", random.random()) # wait for user prompt input("Return to continue") # print 5 random integers between -100 and 100 inclusive print("\nSome random integers between -100 and 100") for i in range(5): print("Random integer #{0:d}".format(i+1), "is", random.randint(-100, 100)) # wait for user prompt input("Return to continue") # now show what seed does random.seed(261) # print 5 random numbers print("\nSome random numbers between 0 and 1 with seed 261") for i in range(5): print("Random number #{0:d}".format(i+1), "is", random.random()) # wait for user prompt input("Return to continue") # now show what seed does random.seed(261) # print 5 more random numbers print("\nSome more random numbers between 0 and 1 with seed 261") for i in range(5): print("Random number #{0:d}".format(i+1), "is", random.random()) # wait for user prompt input("Return to continue") # print 5 choices print("\nPick 1 out of 'cat', 'dog', 'chicken', 'rabbit', 'horse', 'cow'") x = [ 'cat', 'dog', 'chicken', 'rabbit', 'horse', 'cow' ] for i in range(5): print("Random choice #{0:d}".format(i+1), "is", random.choice(x)) # wait for user prompt input("Return to continue") # print 5 choices print("\nPick 2 out of 'cat', 'dog', 'chicken', 'rabbit', 'horse', 'cow'") x = [ 'cat', 'dog', 'chicken', 'rabbit', 'horse', 'cow' ] for i in range(5): print("Random choice #{0:d}".format(i+1), "is", random.choices(x, k=2)) # wait for user prompt input("Return to continue") # shuffle the list print("\nShuffle the list 'cat', 'dog', 'chicken', 'rabbit', 'horse', 'cow'") for i in range(5): x = [ 'cat', 'dog', 'chicken', 'rabbit', 'horse', 'cow' ] random.shuffle(x) print("Shuffle #{0:d}".format(i+1), "is", x)