In []: a *= 3 In []: a In []: # 13. (Discuss and explain.) Part III: Lists and the random module For a game, you might want a program to pick at random from a list. Python doesn’t have a built-in way to get random or pseudo-random numbers. Pseudo-random numbers are unpredictable but not actually random. The computer can use the system clock and some fancy arithmetic to generate a pseudo-random number. Fortunately, someone already wrote the code for generating pseudo-random numbers and shared it. You only need to it from a library included with import Canopy. The module containing these programs is called It has “random”. many functions, as you can see in the Reference” at . There is so much “Library information there that it may be difficult to find what you’re looking for quickly. A time-saving tip is to use the browser’s function to search for the “find” keyword You’ll find documentation on the functions in the random “random”. module by following the link in category 9 as shown below, or just do a Google search for random” and choose the link to the official site. “python import random In []: Here are the three functions we want from the random module. Try each one several times to view random output. Use the up arrow to enter them again. In []: random.choice(values) # choose from a list In []: random.randint(5,8) # choose int from [5,6,7,8] In []: random.uniform(5,8) # choose between 5 and 8 Define a function that simulates rolling two six–sided dice roll_two_dice() and returns the total. Pair program, strategizing first. In []: roll_two_dice() Out[]: 7 Define a function that will pick one letter randomly from the guess_letter() alphabet.
In []: a *= 3 In []: a In []: # 13. (Discuss and explain.) Part III: Lists and the random module For a game, you might want a program to pick at random from a list. Python doesn’t have a built-in way to get random or pseudo-random numbers. Pseudo-random numbers are unpredictable but not actually random. The computer can use the system clock and some fancy arithmetic to generate a pseudo-random number. Fortunately, someone already wrote the code for generating pseudo-random numbers and shared it. You only need to import it from a library included with Canopy. The module containing these programs is called “random”. It has many functions, as you can see in the “Library Reference” at . There is so much information there that it may be difficult to find what you’re looking for quickly. A time-saving tip is to use the browser’s “find” function to search for the keyword “random”. You’ll find documentation on the functions in the random module by following the link in category 9 as shown below, or just do a Google search for “python random” and choose the link to the official site. In []: import random Here are the three functions we want from the random module. Try each one several times to view random output. Use the up arrow to enter them again. In []: random.choice(values) # choose from a list In []: random.randint(5,8) # choose int from [5,6,7,8] In []: random.uniform(5,8) # choose between 5 and 8 Define a function roll_two_dice() that simulates rolling two six–sided dice and returns the total. Pair program, strategizing first. In []: roll_two_dice() Out[]: 7 Define a function guess_letter() that will pick one letter randomly from the alphabet.