一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類(lèi)導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python3 Random模塊代碼詳解

Python3 Random模塊代碼詳解

2020-12-21 00:50llenfeng33 Python

這篇文章主要介紹了Python3 Random模塊代碼詳解,具有一定參考價(jià)值,需要的朋友可以了解下。

描述

random() 方法返回隨機(jī)生成的一個(gè)實(shí)數(shù),它在[0,1)范圍內(nèi)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import random
help(random)
FUNCTIONS
  betavariate(alpha, beta) method of Random instance # 隨機(jī)實(shí)例的方法
    Beta distribution. # β分布
    
    Conditions on the parameters are alpha > 0 and beta > 0. # 必須傳入大于0的alpha 與beta參數(shù)
    Returned values range between 0 and 1. # 返回一個(gè)0 -1 之間的值,這個(gè)值你是隨機(jī)的!
    a = random.betavariate(999999, 99999999999999999) # 第一次執(zhí)行結(jié)果:9.995974671839104e-12 第二次執(zhí)行結(jié)果:1.0006927848540756e-11
    貝塔分布(Beta Distribution) 是一個(gè)作為伯努利分布和二項(xiàng)式分布的共軛先驗(yàn)分布的密度函數(shù),在機(jī)器學(xué)習(xí)和數(shù)理統(tǒng)計(jì)學(xué)中有重要應(yīng)用。
    在概率論中,貝塔分布,也稱(chēng)Β分布,是指一組定義在(0,1) 區(qū)間的連續(xù)概率分布。
  
  choice(seq) method of Random instance
    Choose a random element from a non-empty sequence. # 隨機(jī)從一個(gè)不為空的序列中取出一個(gè)元素,參數(shù)必須不為空
    Python包含 6 種內(nèi)建的序列,包括列表、元組、字符串、Unicode字符串、buffer對(duì)象和xrange對(duì)象。
  
  choices(population, weights=None, *, cum_weights=None, k=1) method of Random instance # 隨機(jī)實(shí)例的方法
    Return a k sized list of population elements chosen with replacement. # 通過(guò)chosen with replacement(就是每次抽取的機(jī)會(huì)都是相同的或按權(quán)重來(lái)的,前面的選擇不會(huì)影響后面選擇的概率)的方式獲取一個(gè)由k個(gè)元素組成的隨機(jī)列表
    
    If the relative weights or cumulative weights are not specified, # 如果未指定相對(duì)權(quán)重或累積權(quán)重,
    the selections are made with equal probability.          # 則選擇的概率相等。
    從population中隨機(jī)抽取k個(gè)元素(可重復(fù))。兩個(gè)參數(shù)不能同時(shí)存在。
    示例:
    print(random.choices(['red', 'black', 'green'], [18, 18, 2], k=6) ) # 以18的概率權(quán)重取red,18的概率權(quán)重取black,2的概率權(quán)重取green,共進(jìn)行6次
    
    trial = lambda: random.choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5 # H的概率是0.6,T的概率是0.4
    print(sum(trial() for i in range(10000)) / 10000)
 
    trial2 = lambda : 2500 <= sorted(random.choices(range(10000), k=5))[2] < 7500
    print(sum(trial2() for i in range(10000)) / 10000 )
 
    from statistics import mean
    data = 1, 2, 4, 4, 10
    means = sorted(mean(random.choices(data, k=5)) for i in range(20)) # mean是求平均
    print(f'The sample mean of {mean(data):.1f} has a 90% confidence '
   f'interval from {means[1]:.1f} to {means[-2]:.1f}') # 這里的f用法
    # 上面程序的三次執(zhí)行結(jié)果分別是:
    # The sample mean of 4.2 has a 90% confidence interval from 2.4 to 6.6
    # The sample mean of 4.2 has a 90% confidence interval from 2.6 to 7.2
    # The sample mean of 4.2 has a 90% confidence interval from 2.0 to 7.0
 
  expovariate(lambd) method of Random instance # 隨機(jī)實(shí)例的方法
    Exponential distribution. # 指數(shù)分布
    
    lambd is 1.0 divided by the desired mean. It should be
    nonzero. (The parameter would be called "lambda", but that is
    a reserved word in Python.) Returned values range from 0 to
    positive infinity if lambd is positive, and from negative
    infinity to 0 if lambd is negative.
    λ(lambd)是1除以所需數(shù)的,它不能為零。(參數(shù)應(yīng)當(dāng)被稱(chēng)為lambda,但那是python保留字)
    這個(gè)函數(shù)返回一個(gè)0到正無(wú)窮大的隨機(jī)數(shù)(如果 λ 為正),或返回一個(gè)負(fù)無(wú)窮大到0的隨機(jī)數(shù),如果(如果 λ 為負(fù))
  
  gammavariate(alpha, beta) method of Random instance # 隨機(jī)實(shí)例的方法
    Gamma distribution. Not the gamma function! # 伽瑪分布.不是gamma函數(shù)
    
    Conditions on the parameters are alpha > 0 and beta > 0. # 參數(shù)的條件是兩個(gè)參數(shù)都要大于0
    
    The probability distribution function is: # 概率分布函數(shù)為:
    
          x ** (alpha - 1) * math.exp(-x / beta)
     pdf(x) = --------------------------------------
           math.gamma(alpha) * beta ** alpha
  
  gauss(mu, sigma) method of Random instance # 隨機(jī)實(shí)例的方法
    Gaussian distribution. # 高斯分布,又名正態(tài)分布或常態(tài)分布
    
    mu is the mean, and sigma is the standard deviation. This is
    slightly faster than the normalvariate() function.
    
    Not thread-safe without a lock around calls.
    # mu為期望,sigma為方差,這個(gè)函數(shù)比normalvariate()函數(shù)速度要快一點(diǎn)點(diǎn)
    # 沒(méi)有線程調(diào)用,線程不安全。
    # 有兩個(gè)必傳參數(shù),mu與sigma,
  
  getrandbits(...) method of Random instance # 隨機(jī)實(shí)例的方法
    getrandbits(k) -> x. Generates an int with k random bits. # 以整型形式返回k個(gè)隨機(jī)位(二進(jìn)制數(shù))。如果處理的是真正的隨機(jī)事務(wù)(比如加密),這個(gè)函數(shù)尤為有用。
  
  getstate() method of Random instance # 隨機(jī)實(shí)例的方法
    Return internal state; can be passed to setstate() later. # 返回捕獲當(dāng)前生成器內(nèi)部狀態(tài)的對(duì)象.該對(duì)象可以用于函數(shù)setstate()來(lái)保存當(dāng)前的狀態(tài).
  
  lognormvariate(mu, sigma) method of Random instance # 隨機(jī)實(shí)例的方法
    Log normal distribution.
    對(duì)數(shù)正態(tài)分布(logarithmic normal distribution)是指一個(gè)隨機(jī)變量的對(duì)數(shù)服從正態(tài)分布,則該隨機(jī)變量服從對(duì)數(shù)正態(tài)分布。
    對(duì)數(shù)正態(tài)分布從短期來(lái)看,與正態(tài)分布非常接近。但長(zhǎng)期來(lái)看,對(duì)數(shù)正態(tài)分布向上分布的數(shù)值更多一些。
    
    If you take the natural logarithm of this distribution, you'll get a
    normal distribution with mean mu and standard deviation sigma.
    mu can have any value, and sigma must be greater than zero.
    # 如果你對(duì)這個(gè)分布的參數(shù)取自然對(duì)數(shù),你會(huì)得到一個(gè)均數(shù)為mu,標(biāo)準(zhǔn)差為sigma的正態(tài)分布。
    # mu參數(shù)可是任何值,sigma參數(shù)必須大于0
  
  normalvariate(mu, sigma) method of Random instance # 隨機(jī)實(shí)例的方法
    Normal distribution. # 常態(tài)分布,又名正態(tài)分布和高斯分布
    
    mu is the mean, and sigma is the standard deviation. # mu為期望,sigma為方差
  
  paretovariate(alpha) method of Random instance # 隨機(jī)實(shí)例的方法
    Pareto distribution. alpha is the shape parameter. # 帕累托分布;參數(shù)alpha為形狀參數(shù)
    # 帕累托分布(Pareto distribution)是以意大利經(jīng)濟(jì)學(xué)家維弗雷多·帕雷托命名的, 是從大量真實(shí)世界的現(xiàn)象中發(fā)現(xiàn)的冪次定律分布,
    # 這個(gè)分布在經(jīng)濟(jì)學(xué)以外,也被稱(chēng)為布拉德福分布。帕累托因?qū)σ獯罄?0%的人口擁有80%的財(cái)產(chǎn)的觀察而著名,后來(lái)被約瑟夫·朱蘭和
    # 其他人概括為帕累托法則(80/20法則),后來(lái)進(jìn)一步概括為帕累托分布的概念。
 
  
  randint(a, b) method of Random instance # 隨機(jī)實(shí)例的方法
    Return random integer in range [a, b], including both end points. # 返回a到b之間的一個(gè)隨機(jī)整數(shù),可取中a和b
    # 必須傳入兩個(gè)參數(shù),且a, b都必須是整數(shù),可以為負(fù)整數(shù),a參數(shù)必須小于等于b參數(shù)。
  
  random(...) method of Random instance # 隨機(jī)實(shí)例的方法
    random() -> x in the interval [0, 1). # 無(wú)參數(shù),生成一個(gè)0-1之間的隨機(jī)數(shù),可以是0,但不會(huì)生成1
  
  randrange(start, stop=None, step=1, _int=<class 'int'>) method of Random instance # 隨機(jī)實(shí)例的方法
    Choose a random item from range(start, stop[, step]). # 隨機(jī)從一個(gè)范圍中選取一個(gè)參數(shù)
    
    This fixes the problem with randint() which includes the # 這個(gè)函數(shù)修復(fù)了randint()中能取到范圍右邊的數(shù)字的情況
    endpoint; in Python this is usually not what you want.  # randint()可以取到右邊范圍數(shù)字的情況通常是很多人不愿意看到的
    參數(shù)必須為整數(shù),start要小于stop參數(shù)
  
  sample(population, k) method of Random instance # 隨機(jī)實(shí)例的方法
    Chooses k unique random elements from a population sequence or set. # 從一個(gè)population序列或集合中取出k個(gè)隨機(jī)元素
    
    Returns a new list containing elements from the population while  # 返還一個(gè)包含上述元素的列表,原序列或集合不會(huì)改變。
    leaving the original population unchanged. The resulting list is
    in selection order so that all sub-slices will also be valid random # 返還的列表是按挑選的先后順序排列的。這樣的話,所有子切片也將是合法的隨機(jī)樣本。
    samples. This allows raffle winners (the sample) to be partitioned # 這樣做允許樣本中的被選中的幸運(yùn)兒能按第一名第二名這樣的順序排列(在返還的列表中)
    into grand prize and second place winners (the subslices).
    
    Members of the population need not be hashable or unique. If the  # population序列中的成員不需要是可哈希的或者是不重樣的。
    population contains repeats, then each occurrence is a possible   # 如果population序列包含重復(fù)的成員,那么每次的選取都會(huì)是樣本中可能的選擇
    selection in the sample.
    
    To choose a sample in a range of integers, use range as an argument # 為了在一個(gè)整數(shù)范圍內(nèi)選擇一個(gè)樣本,請(qǐng)使用范圍值作為一個(gè)參數(shù)。
    This is especially fast and space efficient for sampling from a   # 當(dāng)從龐大數(shù)據(jù)中取樣時(shí)這樣做將會(huì)非常快速并且節(jié)省內(nèi)存
    large population:  sample(range(10000000), 60)           # 示例:sample(range(10000000), 60)
  
  seed(a=None, version=2) method of Random instance # 隨機(jī)實(shí)例的方法
    Initialize internal state from hashable object.          # 通過(guò)可哈希對(duì)象初始化內(nèi)部狀態(tài)
    
    None or no argument seeds from current time or from an operating # 參數(shù)為None或無(wú)參數(shù)的情況下,則seeds根據(jù)當(dāng)前時(shí)間來(lái)自己選擇這個(gè)值
    system specific randomness source if available.          # 或從系統(tǒng)特定的隨機(jī)性源中取值(如果條件可行)
    
    If *a* is an int, all bits are used.               # 如果參數(shù)a為整型,所有的bits位都將被使用
    
    For version 2 (the default), all of the bits are used if *a* is a str, # 當(dāng)version參數(shù)為2(默認(rèn)參數(shù)),如果參數(shù)a是一個(gè)字符串類(lèi)型,bytes或bytearray,則所有的bits位都將被使用
    bytes, or bytearray. For version 1 (provided for reproducing random  # 當(dāng)version參數(shù)為1時(shí)(提供從老版本python中重新生成的隨機(jī)序列)
    sequences from older versions of Python), the algorithm for str and   # 通過(guò)對(duì)str,bytes的算法生成一個(gè)窄范圍的種子。
    bytes generates a narrower range of seeds.
  
  setstate(state) method of Random instance # 隨機(jī)實(shí)例的方法
    Restore internal state from object returned by getstate(). # 保存getstate()取得并返回的對(duì)象。
  
  shuffle(x, random=None) method of Random instance # 隨機(jī)實(shí)例的方法
    Shuffle list x in place, and return None. # 打亂列表x并返回None x為必傳列表參數(shù)
    
    Optional argument random is a 0-argument function returning a # 可選參數(shù)為一個(gè)不需參數(shù)的返回0-1之間浮點(diǎn)數(shù)的函數(shù)
    random float in [0.0, 1.0); if it is the default None, the   # 如果可選參數(shù)為默認(rèn)的None,則它會(huì)使用random.random函數(shù)方法
    standard random.random will be used.
    如果你有一個(gè)更好的隨機(jī)數(shù)生成器,或者說(shuō)你有一個(gè)適合自己應(yīng)用的隨機(jī)數(shù)發(fā)生器,那么你就可以使用它而不是使用系統(tǒng)默認(rèn)那個(gè)。
  
  triangular(low=0.0, high=1.0, mode=None) method of Random instance # 隨機(jī)實(shí)例的方法
    Triangular distribution. # 三角分布(triangular distribution),亦稱(chēng)辛普森分布或三角形分布
    
    Continuous distribution bounded by given lower and upper limits, # 三角形分布是低限為low、上限為high、眾數(shù)默認(rèn)為兩者平均數(shù)的連續(xù)概率分布。
    and having a given mode value in-between.
    
    http://en.wikipedia.org/wiki/Triangular_distribution
  
  uniform(a, b) method of Random instance # 隨機(jī)實(shí)例的方法
    Get a random number in the range [a, b) or [a, b] depending on rounding. # 從a與b之間取一個(gè)隨機(jī)數(shù),一定可以取到a,b能否取到看b的舍入
    a b兩個(gè)參數(shù)必須有,可為整型可為浮點(diǎn)型,目前本人知道的取到b的方法的用math.ceil
    這個(gè)方法返回的是一個(gè)隨機(jī)數(shù)
  
  vonmisesvariate(mu, kappa) method of Random instance # 隨機(jī)實(shí)例的方法
    Circular data distribution. # 循環(huán)數(shù)據(jù)分布或馮·米塞斯分布
    
    mu is the mean angle, expressed in radians between 0 and 2*pi, and
    kappa is the concentration parameter, which must be greater than or
    equal to zero. If kappa is equal to zero, this distribution reduces
    to a uniform random angle over the range 0 to 2*pi.
    # mu 是位置的度量,它的聚會(huì)在0-2*pi之間,kappa是集中度的度量,它必須大于或等于0。
    # 如果kappa等于0,這個(gè)分布是均勻分布,對(duì)于κ很小的情形,分布近似均勻分布,其位置度量在0-2*pi之間
 
  
  weibullvariate(alpha, beta) method of Random instance
    Weibull distribution. # 韋布爾分布
    
    alpha is the scale parameter and beta is the shape parameter. # λ>0是比例參數(shù)(scale parameter),k>0是形狀參數(shù)(shape parameter)
    # 在這里alpha是比例參數(shù),beta是形狀參數(shù)
    威布爾分布(Weibull distribution),又稱(chēng)韋伯分布或韋布爾分布,是可靠性分析和壽命檢驗(yàn)的理論基礎(chǔ)。
    威布爾分布:在可靠性工程中被廣泛應(yīng)用,尤其適用于機(jī)電類(lèi)產(chǎn)品的磨損累計(jì)失效的分布形式。由于它可以利用概率值很容易地推斷出它的分布參數(shù),
    被廣泛應(yīng)用于各種壽命試驗(yàn)的數(shù)據(jù)處理。

總結(jié)

以上就是本文關(guān)于Python3 Random模塊代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

原文鏈接:http://www.cnblogs.com/fengbo1113/p/7967393.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青草久久影院 | 日韩欧美一区二区三区免费观看 | 欧美a一级片 | 日本69sex护士www | 欧美一级精品 | 美女的隐私无遮挡的网页 | 成人高辣h视频一区二区在线观看 | 免费在线看 | 天天草b | 成功精品影院 | 午夜理论片YY4399影院 | 爱情岛论坛亚洲一号路线 | 精品无人乱码一区二区三区 | 国产成人黄网在线免 | 美女污视频在线观看 | 欧美一级艳片视频免费观看 | 99热资源| 无人在线视频高清免费播放 | 四虎成人免费观看在线网址 | 污文啊好棒棒啊好了 | 免费看一级大片 | 天天插在线视频 | 村妇超级乱淫伦小说全集 | 日本精品一区二区在线播放 | 欧美国产在线观看 | 成人网18免费网站 | 亚洲国产99在线精品一区69堂 | 亚洲欧美日韩国产精品一区 | caoporn超碰最新地址进入 | 国产精品女主播大秀在线 | 青青热久麻豆精品视频在线观看 | 日韩成人影视 | 亚洲 欧美 中文 日韩 另类 | 1313午夜精品久久午夜片 | 午夜爽喷水无码成人18禁三级 | 韩国三级年轻的小婊孑 | 好紧好爽再叫浪一点点潘金莲 | 魔兽官方小说 | 性白俄罗斯高清xxxxx | 不知火舞被c视频在线播放 不卡一区二区三区卡 | 全黄h全肉细节文在线观看 全彩成人18h漫画 |