Using Python 'random' module
The 'random' module is a built-in Python module that provides functions to generate random numbers.
You can use the random.randint(a, b)
, random.uniform(a, b)
, and random.random()
functions to generate random number in Python.
You need to import random module to use its functions using the code (import random
) where 'import' is a keyword and 'random' is the name of the module.
Getting a random integer number using random.randin() function
1. Use random.randint(a, b) function to get a random integer number. In the function, 'a' represents the lowest number and 'b' represents the highest number of the integer number range. In the following example, the code "random_integer = random.randint(100, 999)
" is used to get a random integer number ranging from 100 to 999.
Example of using random.randint() function
To check the output of the above example code, please try the code in Live Code Playground.
Getting a random float number using random.uniform() function
1. Use random.uniform(a, b) function to get a random float number. In the function, 'a' represents the lowest float number and 'b' represents the highest float number of the float number range. In the following example, the code "random_float = random.uniform(1.0, 99.0)
" is used to get a random float number ranging from 1.0 to 99.0.
Example of using random.uniform() function
To check the output of the above example code, please try the code in Live Code Playground.
Getting a random number using random.random() function
1. Use random.random() function to get a random float number between 0 and 1. In the following example, the code "random_float = random.random()
" is used to get a random float number between 0 and 1.