PHP前端开发

区别python中randrange()和uniform()的小技巧

百变鹏仔 3小时前 #Python
文章标签 小技巧

从函数签名中我们可以知道:

In [7]: random.randrange?Signature: random.randrange(start, stop=None, step=1, _int=<type>, _maxwidth=9007199254740992L)Docstring:Choose a random item from range(start, stop[, step]).This fixes the problem with randint() which includes theendpoint; in Python this is usually not what you want.File:      /usr/lib/python2.7/random.pyType:      instancemethodIn [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.pyType:      instancemethod</type>

randrange 是从 range(start, stop[, step]) 随机挑选一个,生成的一定是 int ;
uniform 是从 [a, b) 或 [a, b] 中生成一个随机数,生成的是 float;
这两个使用场景不一样。