Latest web development tutorials

Python random () function

Python figures Python figures


description

random () method returns a real number randomly generated within it [0,1) range.


grammar

The following is the syntax for random () method:

import random

random.random()

Note: random () is not directly accessible, you need to import the random module, and then call the method by random static objects.


parameter

  • no

return value

Returns a real number randomly generated, it is within the [0,1) range.

Examples

The following illustrates the use of random () method Example:

#!/usr/bin/python
import random

# 生成第一个随机数
print "random() : ", random.random()

# 生成第二个随机数
print "random() : ", random.random()

After running the above example output is:

random() :  0.281954791393
random() :  0.309090465205

Python figures Python figures