python中uniform函数_在python中的randrange函数和uniform函数的区别是什么

优质回答 回答者:果窝

函数签名说的已经比较清楚了啊:

In [7]: random.randrange?

Signature: random.randrange(start, stop=None, step=1, _int=, _maxwidth=9007199254740992L)

Docstring:

Choose a random item from range(start, stop[, step]).

This fixes the problem with randint() which includes the

endpoint; in Python this is usually not what you want.

File: /usr/lib/python2.7/random.py

Type: instancemethod

In [8]: random.uniform?

Signature: random.uniform(a, b)

Docstring: Get a random number in the range [a, b) or [a, b] depending on rounding.

File: /usr/lib/python2.7/random.py

Type: instancemethod

randrange 是从 range(start, stop[, step]) 随机挑选一个,生成的一定是 int ;

uniform 是从 [a, b) 或 [a, b] 中生成一个随机数,生成的是 float;

这两个使用场景完全不一样啊,没有什么相互替代的说法