
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How does Python generate random numbers?
Python includes a built-in package i.e random module for generating random numbers.
In this article, we will show you how to generate random numbers in python using different methods −
Using random.seed() method
Using random.randrange() method
Using random.randint() method
Using random.random()
Using random.choice() method
Using random.uniform() method
Method 1: Using random.seed() method
The random number generator is initialized using the seed() method.
To generate a random number, the random number generator requires a starting numeric value (a seed value).
NOTE − The random number generator utilizes the current system time by default.
To change the random number generator's starting number, use the seed() method.
You will get the same random number if you use the same seed value twice.
Syntax
random.seed(x, version)
Parameters
x(optional) − The seed value required to generate a random number. If it is an integer, it is utilized directly; otherwise, it must be converted to an integer.
version − An integer that specifies how to convert the ‘x’ argument to an integer. The default value is 2. The generator uses the current system time if the value is None.
The following program returns a random element from the list using random.random() and seed() methods −
import random # Setting the seed value to 5 random.seed(5) print(random.random()) # Setting the same seed value as above i.e 5 random.seed(5) print(random.random())
Output
On executing, the above program will generate the following output −
0.62290169489 0.62290169489
We set the seed value to 5 and then used the random() function of the random module to generate a random value. Then we set the seed value to 5 again and used the random() function to generate the same random value as before. This is how the seed() function is used.
Method 2: Using random.randrange() method
The randrange() method selects an element at random from the specified range and returns it.
Syntax
random.randrange(start, stop, step)
Parameters
start(optional) − An integer indicating the starting position. 0 is the default
stop(required) − An integer indicating the end position.
step(optional) − It is an integer indicating the incrementation. 1 by default.
The following program returns the random number between the given range using the randrange() function −
import random # generating a random number between 10(included) and 20(not included) print("Random Number Generated = ", random.randrange(10, 20))
Output
On executing, the above program will generate the following output −
Random Number Generated = 13
The randrange() function is used here to generate a random number from a range. We passed the starting value (lower limit) and ending value (upper limit) as arguments, and it generated a random number between those ranges
Method 3: Using random.randint() method
The randint() method returns an integer value representing a randomly chosen element from the given range.
NOTE − The randint() method is an alias for randrange(start, stop+1).
Syntax
random.randint(start, stop)
Parameters
start(required) − An integer indicating the starting position.
stop(required) − An integer indicating the end position.
The following program returns the random number between the given range using the randint() function−
import random # generating a random number between 10 and 20(both 10 and 20 numbers included) print("Random Number Generated = ", random.randint(10, 20))
Output
On executing, the above program will generate the following output −
Random Number Generated = 20
The difference between randrange() and randint is that randint includes the upper limit in the range, whereas randrange() excludes the upper limit. We can add a step value to the randrange() function but not to the randint() function.
Method 4: Using random.random()
The random() method generates a random floating-point value between 0 and 1.
Syntax
random.random()
Parameters
This random() method doesn’t accept any arguments
The following program returns the random number between the given range using the random() function −
import random # generating a random floating-point number between 0 and 1 print("Random Number Generated = ", random.random())
Output
Random Number Generated = 0.15685132230226662
Method 5: Using random.choice() method
The choices() method returns a list containing the element from the provided sequence that was chosen at random.
The sequence could be a string, a range, a list, a tuple, or anything else.
The following program returns the random number from the list −
import random #Input list given_List = [ 1, 6, 3, 9, 10, 24, 475, 483, 2656] print('The first Random Element from the list:',random.choice(given_List)) print('The Second Random Element from the list:',random.choice(given_List))
Output
The first Random Element from the list: 6 The Second Random Element from the list: 24
Method 6: Using random.uniform() method
The uniform() method generates a random floating number between the two input values (both numbers included).
Syntax
random.uniform(x, y)
Parameters
x(required) − number representing the lowest possible outcome
y(required) − number representing the highest possible outcome
The following program returns the random floating number between the given range using the uniform() function −
import random # generating a random number between 10 and 20(both 10 and 20 are also included) print("Random Number Generated = ", random.uniform(10, 20))
Output
Random Number Generated = 12.845596876772472
In this case, the uniform() function is used to generate a random floating number from a range. We gave it the starting (lower limit) and ending (upper limit) values as arguments, and it generated a random float/decimal number between those two ranges.
Conclusion
We learned how to generate a random number using six different methods in this article:
seed(),randrange(),randint(),choice(),random(),uniform() . We also learned how to use the choice() function to get a random element from a list/string/tuple.
- Related Articles
- Generate pseudo-random numbers in Python
- How to generate non-repeating random numbers in Python?
- How to use Python Numpy to generate Random Numbers?
- Python module to Generate secure random numbers
- Generate random numbers in Arduino
- Generate Secure Random Numbers for Managing Secrets using Python
- Generate random numbers using C++11 random library
- How to generate random numbers between two numbers in JavaScript?
- How to generate large random numbers in Java?
- Java program to generate random numbers
- Generate Random Integer Numbers in Java
- How to generate standard normal random numbers in R?
- Generate Random Long type numbers in Java
- Java Program to generate random numbers string
- Generate random characters and numbers in JavaScript?
