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

Python3 uniform() Function

Python3 uniform() Function

Python3 Numbers Python3 Numbers


Description

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


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/python3

import

random

print

(

"

Random float from uniform(5, 10):

"

,

random

.

uniform

(

5

,

10

)

)

print

(

"

Random float from uniform(7, 14):

"

,

random

.

uniform

(

7

,

14

)

)

Running the example above produces:


uniform(5, 10) 的随机浮点数 :  7.054602800254241
uniform(7, 14) 的随机浮点数 :  12.552229882744296

Python3 Numbers Python3 Numbers