Python uniform() Function
Description
uniform()The method will randomly generate the next real number, which is in the[x, y]range.
Syntax
The following is the syntax for the uniform() method:
import random random.uniform(x, y)
Note:uniform() cannot be accessed directly; you need to import the random module and then call the method through the random static object.
Parameters
- x -- The minimum value of the random number, inclusive.
- y -- The maximum value of the random number, inclusive.
Return Value
Returns a floating-point number N, the value range is ifx<yThenx <= N <= y, ify<xTheny <= N <= x。
Example
The following demonstrates an example of using the uniform() method:
Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
print "The random number for uniform(5, 10) is:", random.uniform(5, 10)
print "The random number for uniform(7, 14) is:", random.uniform(7, 14)
After running the above example, the output result is:
uniform(5, 10) 的随机数为 : 6.98774810047 uniform(7, 14) 的随机数为 : 12.2243345905Other Extensions
Python Numbers