Read Aloud the Text Content
This audio was created by Woord's Text to Speech service by content creators from all around the world.
Text Content or SSML code:
In some simulations, the computer must randomly decide how a person, animal, insect, or other living being will behave. Formulas can be constructed in which a random number is used to determine various actions and events that take place in the program. Random numbers are useful in statistical programs that must randomly select data for analysis. Random numbers are commonly used in computer security to encrypt sensitive data. Python provides several library functions for working with random numbers. These functions are stored in a module named random in the standard library. To use any of these functions, you first need to write this import statement at the top of your program: import random This statement causes the interpreter to load the contents of the random module into memory. This makes all of the functions in the random module available to your program.3 The first random-number generating function that we will discuss is named randint. Because the randint function is in the random module, we will need to use dot notation to refer to it in our program. In dot notation, the function’s name is random.randint. On the left side of the dot (period) is the name of the module, and on the right side of the dot is the name of the function. The following statement shows an example of how you might call the randint function: number = random.randint (1, 100) The part of the statement that reads random.randint(1, 100) is a call to the randint function. Notice two arguments appear inside the parentheses: 1 and 100. These arguments tell the function to give an integer random number in the range of 1 through 100. (The values 1 and 100 are included in the range.) Figure 5-20 illustrates this part of the statement. Figure 5-20 A statement that calls the random function The figure illustrates a statement that calls the random function Line 1: number equals random dot randint open round bracket 1 comma 100 close round bracket. Note: 1 and 100 are labeled arguments. The right side of equal sign is labeled Function call. Notice the call to the randint function appears on the right side of an = operator. When the function is called, it will generate a random number in the range of 1 through 100 then return that number. The number that is returned will be assigned to the number variable, as shown in Figure 5-21. Figure 5-21 The random function returns a value The figure illustrates the return value of a random function. Line 1: number equals random dot randint open round bracket 1 comma 100 close round bracket. Note: An arrow from randint points to number on the left side and is labeled some number. A random number in the range of 1 through 100 will be assigned to the number variable. Program 5-18 shows a complete program that uses the randint function. The statement in line 7 generates a random number in the range of 1 through 10 and assigns it to the number variable. (The program output shows that the number 7 was generated, but this value is arbitrary. If this were an actual program, it could display any number from 1 to 10.) Program 5-18 (random_numbers.py) # This program displays a random number # in the range of 1 through 10. import random def main(): # Get a random number. number = random.randint(1, 10) # Display the number. print(f'The number is {number}.') # Call the main function. main() Program Output The number is 7. Program 5-19 shows another example. This program uses a for loop that iterates five times. Inside the loop, the statement in line 8 calls the randint function to generate a random number in the range of 1 through 100. Program 5-19 (random_numbers2.py) # This program displays five random # numbers in the range of 1 through 100. import random def main(): for count in range(5): # Get a random number. number = random.randint(1, 100) # Display the number. print(number) # Call the main function. main() Program Output 89 7 16 41 12 Start Animation