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

Note

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.

© Copyright 2024 GS Ng.

Next Section - 3.1 Text Input and Output