Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Random – random() Function

  • Last Updated : 08 Oct, 2021

Python random.random() function generate random floating numbers between 0 and 1.

Syntax : random.random()

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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

Example 1: Simple implementation of sample() function.

Python3




# Python3 program to demonstrate 
# the use of random() function . 
    
# import random  
from random import random 
    
# Prints random item 
print(random())

Output:

0.41941790721207284

Example 2: Python random.random range

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] 

Example 3: Python random.random() seed

Python3




import random
  
random.seed(10)
print(random.random())

Output:

0.5714025946899135



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!