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

Python uniform() function

Python uniform() function

Python Numbers Python Numbers


Description

uniform() The method randomly generates the next real number in the range [x, y].


Syntax

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



import random



random.uniform(x, y)

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


Parameters


Return Value

Returns a float N in the range if x then x <= N <= y, if y then y <= N <= x.


Example

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

Example

#!/usr/bin/python

-- coding: UTF-8 --

import

random

print

"

The random number from uniform(5, 10) is:

"

,

random

.

uniform

(

5

,

10

)

print

"

The random number from uniform(7, 14) is:

"

,

random

.

uniform

(

7

,

14

)

Running the example above produces:



uniform(5, 10) 的随机数为 :  6.98774810047

uniform(7, 14) 的随机数为 :  12.2243345905

Python Numbers Python Numbers