Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python random() function

Python random() function

Python Numbers Python Numbers


Description

random() The method returns a randomly generated real number in the range [0,1).


Syntax

The syntax of the random() method is as follows:


import random

random.random()

**Note:** random() cannot be accessed directly; you need to import the random module and then call the method through the random static object.


Parameters


Return Value

Returns a randomly generated real number in the range [0,1).


Example

The following shows examples of using the random() method:

Example

#!/usr/bin/python

-- coding: UTF-8 --

import

random

Generate the first random number

print

"

random() :

"

,

random

.

random

(

)

Generate the second random number

print

"

random() :

"

,

random

.

random

(

)

Running the example above produces:


random() :  0.281954791393
random() :  0.309090465205

Python Numbers Python Numbers