Python Random - random() Function
Last Updated :
26 Apr, 2023
There are certain situations that involve games or simulations which work on a non-deterministic approach. In these types of situations, random numbers are extensively used in the following applications:
- Creating pseudo-random numbers on Lottery scratch cards
- reCAPTCHA on login forms uses a random number generator to define different numbers and images
- Picking a number, flipping a coin, and throwing of a dice related games required random numbers
- Shuffling deck of playing cards
In Python, random numbers are not generated implicitly; therefore, it provides a random module in order to generate random numbers explicitly. A random module in Python is used to create random numbers. To generate a random number, we need to import a random module in our program using the command:
import random
Python Random random() Method
The random.random() function generates random floating numbers in the range of 0.1, and 1.0. It takes no parameters and returns values uniformly distributed between 0 and 1. There are various functions associated with the random module are:
- Python random()
- Python randrange()
- Python randint()
- Python seed()
- Python choice(), and many more. We are only demonstrating the use of the random() function in this article.
Python Random random() Syntax
Syntax : random.random()
Parameters : This method does not accept any parameter.
Returns : This method returns a random floating number between 0 and 1.
Python random.random() Method Example
Random in Python generate different number every time you run this program.
Python3
# Python3 program to demonstrate
# the use of random() function .
# import random
from random import random
# Prints random item
print(random())
Output:
0.41941790721207284
Another way to write the same code.
Python3
# Python3 program to demonstrate
# the use of random() function .
import random
# Prints random item
print(random.random())
Output:
0.059970593824388185
Create a List of Random Numbers
The random() method in Python from the random module generates a float number between 0 and 1. Here, we are using Python Loop and append random numbers in the Python list.
Python3
# Python3 program to demonstrate
# the use of random() function .
# import random
from random import random
lst = []
for i in range(10):
lst.append(random())
# Prints random items
print(lst)
Output:
[0.12144204979175777, 0.27614050014306335, 0.8217122381411321, 0.34259785168486445, 0.6119383347065234, 0.8527573184278889, 0.9741465121560601, 0.21663626227016142, 0.9381166706029976, 0.2785298315133211]
Python Random seed() Method
This function generates a random number based on the seed value. It is used to initialize the base value of the pseudorandom number generator. If the seed value is 10, it will always generate 0.5714025946899135 as the first random number.
Python3
import random
random.seed(10)
print(random.random())
#Printing the random number twice
random.seed(10)
print(random.random())
Output:
0.5714025946899135
0.5714025946899135
Similar Reads
Python Random Module Python Random module generates random numbers in Python. These are pseudo-random numbers means they are not truly random. This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc. It is an in-built function in Python.Appli
6 min read
Python - random.seed( ) method random.seed() method in Python is used to initialize the random number generator, ensuring the same random numbers on every run. By default, Python generates different numbers each time, but using .seed() allows result reproducibility.It's most commonly used in:Machine Learning- to ensure model cons
4 min read
random.getstate() in Python random() module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.getstate() The getstate() method of the random module returns an object with the curr
1 min read
random.setstate() in Python Random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.setstate() The setstate() method of the random module is used in conjugation with the g
2 min read
random.getrandbits() in Python random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.getrandbits() The getrandbits() method of the random module is used to return an intege
1 min read
randrange() in Python The randrange() function in Python's random module is used to generate a random number within a specified range. It allows defining a start, stop, and an optional step value to control the selection of numbers. Unlike randint(), which includes the upper limit, randrange() excludes the stop value. Ex
4 min read
randint() Function in Python randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions one of them being able to generate random numbers, which is randint(). In this article, we will learn about randint in Python.Python randint() Method SyntaxSyntax: randint(sta
6 min read
Python Numbers | choice() function choice() is an inbuilt function in Python programming language that returns a random item from a list, tuple, or string. Syntax: random.choice(sequence) Parameters: sequence is a mandatory parameter that can be a list, tuple, or string. Returns: The choice() returns a random item. Note:We have to im
1 min read
Python - random.choices() method The choices() method returns multiple random elements from the list with replacement. Unlike random.choice(), which selects a single item, random.choices() allows us to select multiple items making it particularly useful for tasks like sampling from a population or generating random data.Example:Pyt
3 min read
random.sample() function - Python sample() is an built-in function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Example:Pythonfrom random import sample a = [1, 2, 3, 4, 5] print(sample(a,3))Output[2, 5
2 min read