區別python中randrange()和uniform()的小技巧

Y2J
發布: 2017-05-17 13:19:09
原創
2686 人瀏覽過

從函式簽章中我們可以知道:

In [7]: random.randrange?
Signature: random.randrange(start, stop=None, step=1, _int=<type &#39;int&#39;>, _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;
這兩個使用場景不一樣。

【相關推薦】

1.特別推薦「php程式設計師工具箱」V0.1版本下載

2. Python免費影片教學

#3. Python基礎之uniform()的詳解

以上是區別python中randrange()和uniform()的小技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!