Chapter 2 Using Python as a Calculator
2.10 Randoms Numbers¶
For one last example of modules, the random module provides functions for generating random numbers. It is useful to a range of mathematical problems, including a branch of numerical simulation methods that will be introduced in this book.
Two important functions provided by the random module include random.random and random.randint. random.random() takes no input arguments, and returns a random floating point number in the range (0.0, 1.0). random.randint(a, b) takes two integer input arguments a and b, and returns a random number between a and b, inclusive.
In general, many commonly-used mathematical functions are provided by Python, and it would always be useful to look for a readily-available implementation via the Python documentation and other resources. However, there are also cases where a customized function is needed. The following chapters will introduce step by step how complex functionalities can be achieved by the powerful Python language.
Check your understanding
- the math module
- While you might want to use the math module for other numerical computations in your program, it does not contain functions that are likely to help you simulate a dice roll.
- the random module
- You would likely call the function random.randrange.
- the turtle module
- The turtle module, while producing interesting graphics, is unlikely to help you here.
- the game module
- Python does not have a game module.
2.10Q-1: Which module would you most likely use if you were writing a function to simulate rolling dice?
- prob = random.randint(1, 101)
- This will generate a number between 1 and 101, inclusive.
- prob = random.randint(1, 100)
- This will generate a number between 1 and 100, inclusive.
- prob = random.randint(0, 101)
- This will generate a number between 0 and 101, inclusive.
- prob = random.randint(0, 100)
- This will generate a number between 0 and 100, inclusive.
2.10Q-2: The correct code to generate a random number between 1 and 100 (inclusive) is:
- There is no computer on the stage for the drawing.
- They could easily put one there.
- Because computers don’t really generate random numbers, they generate pseudo-random numbers.
- Computers generate random numbers using a deterministic algorithm. This means that if anyone ever found out the algorithm they could accurately predict the next value to be generated and would always win the lottery.
- They would just generate the same numbers over and over again.
- This might happen if the same seed value was used over and over again, but they could make sure this was not the case.
- The computer can’t tell what values were already selected, so it might generate all 5’s instead of 5 unique numbers.
- While a programmer would need to ensure the computer did not select the same number more than once, it is easy to ensure this.
2.10Q-3: One reason that lotteries don’t use computers to generate random numbers is:
Note
This workspace is provided for your convenience. You can use this activecode window to try out anything you like.
© Copyright 2024 GS Ng.