Python3 Random模組,python3random模組

來源:互聯網
上載者:User

Python3 Random模組,python3random模組

import randomhelp(random)FUNCTIONS    betavariate(alpha, beta) method of Random instance  # 隨機執行個體的方法        Beta distribution.  # β分布                Conditions on the parameters are alpha > 0 and beta > 0.  # 必須傳入大於0的alpha 與beta參數        Returned values range between 0 and 1.  # 返回一個0 -1 之間的值,這個值你是隨機的!        a = random.betavariate(999999, 99999999999999999)  # 第一次執行結果:9.995974671839104e-12  第二次執行結果:1.0006927848540756e-11        貝塔分布(Beta Distribution) 是一個作為伯努利分布和二項式分布的共軛先驗分布的密度函數,在機器學習和數理統計學中有重要應用。        在機率論中,貝塔分布,也稱Β分布,是指一組定義在(0,1) 區間的連續機率分布。        choice(seq) method of Random instance        Choose a random element from a non-empty sequence.  # 隨機從一個不為空白的序列中取出一個元素,參數必須不為空白        Python包含 6 種內建的序列,包括列表、元組、字串、Unicode字串、buffer對象和xrange對象。        choices(population, weights=None, *, cum_weights=None, k=1) method of Random instance  # 隨機執行個體的方法        Return a k sized list of population elements chosen with replacement.  # 通過chosen with replacement(就是每次抽取的機會都是相同的或按權重來的,前面的選擇不會影響後面選擇的機率)的方式擷取一個由k個元素組成的隨機列表                If the relative weights or cumulative weights are not specified,  # 如果未指定相對權重或累積權重,        the selections are made with equal probability.                   # 則選擇的機率相等。        從population中隨機抽取k個元素(可重複)。兩個參數不能同時存在。        樣本:        print(random.choices(['red', 'black', 'green'], [18, 18, 2], k=6) )  # 以18的機率權重取red,18的機率權重取black,2的機率權重取green,共進行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用法         # 上面程式的三次執行結果分別是:        # 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  # 隨機執行個體的方法        Exponential distribution.  # 指數分布                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除以所需數的,它不能為零。(參數應當被稱為lambda,但那是python保留字)        這個函數返回一個0到正無窮大的隨機數(如果 λ 為正),或返回一個負無窮大到0的隨機數,如果(如果 λ 為負)        gammavariate(alpha, beta) method of Random instance  # 隨機執行個體的方法        Gamma distribution.  Not the gamma function!  # 伽瑪分布.不是gamma函數                Conditions on the parameters are alpha > 0 and beta > 0.  # 參數的條件是兩個參數都要大於0                The probability distribution function is:  # 機率分布函數為:                            x ** (alpha - 1) * math.exp(-x / beta)          pdf(x) =  --------------------------------------                      math.gamma(alpha) * beta ** alpha        gauss(mu, sigma) method of Random instance  # 隨機執行個體的方法        Gaussian distribution.  # 高斯分布,又名常態分佈或常態分布                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為方差,這個函數比normalvariate()函數速度要快一點點        # 沒有線程調用,線程不安全。        # 有兩個必傳參數,mu與sigma,        getrandbits(...) method of Random instance  # 隨機執行個體的方法        getrandbits(k) -> x.  Generates an int with k random bits.  # 以整型形式返回k個隨機位(位元)。如果處理的是真正的隨機事務(比如加密),這個函數尤為有用。        getstate() method of Random instance  # 隨機執行個體的方法        Return internal state; can be passed to setstate() later.  # 返回捕獲當前產生器內部狀態的對象.該對象可以用於函數setstate()來儲存當前的狀態.        lognormvariate(mu, sigma) method of Random instance  # 隨機執行個體的方法        Log normal distribution.        對數常態分佈(logarithmic normal distribution)是指一個隨機變數的對數服從常態分佈,則該隨機變數服從對數常態分佈。        對數常態分佈從短期來看,與常態分佈非常接近。但長期來看,對數常態分佈向上分布的數值更多一些。                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.        # 如果你對這個分布的參數取自然對數,你會得到一個均數為mu,標準差為sigma的常態分佈。        # mu參數可是任何值,sigma參數必須大於0        normalvariate(mu, sigma) method of Random instance  # 隨機執行個體的方法        Normal distribution.  # 常態分布,又名常態分佈和高斯分布                mu is the mean, and sigma is the standard deviation.  # mu為期望,sigma為方差        paretovariate(alpha) method of Random instance  # 隨機執行個體的方法        Pareto distribution.  alpha is the shape parameter.  # 帕累托分布;參數alpha為形狀參數        # 帕累托分布(Pareto distribution)是以意大利經濟學家維弗雷多·帕雷託命名的, 是從大量真實世界的現象中發現的冪次定律分布,        # 這個分布在經濟學以外,也被稱為布拉德福分布。帕累托因對意大利20%的人口擁有80%的財產的觀察而著名,後來被約瑟夫·朱蘭和        # 其他人概括為帕累托法則(80/20法則),後來進一步概括為帕累托分布的概念。        randint(a, b) method of Random instance  # 隨機執行個體的方法        Return random integer in range [a, b], including both end points.  # 返回a到b之間的一個隨機整數,可取中a和b        # 必須傳入兩個參數,且a, b都必須是整數,可以為負整數,a參數必須小於等於b參數。        random(...) method of Random instance  # 隨機執行個體的方法        random() -> x in the interval [0, 1).  # 無參數,產生一個0-1之間的隨機數,可以是0,但不會產生1        randrange(start, stop=None, step=1, _int=<class 'int'>) method of Random instance  # 隨機執行個體的方法        Choose a random item from range(start, stop[, step]).  # 隨機從一個範圍中選取一個參數                This fixes the problem with randint() which includes the  # 這個函數修複了randint()中能取到範圍右邊的數位情況        endpoint; in Python this is usually not what you want.    # randint()可以取到右邊範圍數位情況通常是很多人不願意看到的        參數必須為整數,start要小於stop參數        sample(population, k) method of Random instance  # 隨機執行個體的方法        Chooses k unique random elements from a population sequence or set. # 從一個population序列或集合中取出k個隨機元素                Returns a new list containing elements from the population while    # 返還一個包含上述元素的列表,原序列或集合不會改變。        leaving the original population unchanged.  The resulting list is         in selection order so that all sub-slices will also be valid random # 返還的列表是按挑選的先後順序排列的。這樣的話,所有子切片也將是合法的隨機樣本。        samples.  This allows raffle winners (the sample) to be partitioned # 這樣做允許樣本中的被選中的幸運兒能按第一名第二名這樣的順序排列(在返還的列表中)        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序列包含重複的成員,那麼每次的選取都會是樣本中可能的選擇        selection in the sample.                To choose a sample in a range of integers, use range as an argument # 為了在一個整數範圍內選擇一個樣本,請使用範圍值作為一個參數。        This is especially fast and space efficient for sampling from a     # 當從龐大資料中取樣時這樣做將會非常快速並且節省記憶體        large population:   sample(range(10000000), 60)                     # 樣本:sample(range(10000000), 60)        seed(a=None, version=2) method of Random instance  # 隨機執行個體的方法        Initialize internal state from hashable object.                   # 通過可雜湊對象初始化內部狀態                None or no argument seeds from current time or from an operating  # 參數為None或無參數的情況下,則seeds根據目前時間來自己選擇這個值        system specific randomness source if available.                   # 或從系統特定的隨機性源中取值(如果條件可行)                If *a* is an int, all bits are used.                              # 如果參數a為整型,所有的bits位都將被使用                For version 2 (the default), all of the bits are used if *a* is a str,  # 當version參數為2(預設參數),如果參數a是一個字串類型,bytes或bytearray,則所有的bits位都將被使用        bytes, or bytearray.  For version 1 (provided for reproducing random    # 當version參數為1時(提供從老版本python中重建的隨機序列)        sequences from older versions of Python), the algorithm for str and     # 通過對str,bytes的演算法產生一個窄範圍的種子。        bytes generates a narrower range of seeds.        setstate(state) method of Random instance  # 隨機執行個體的方法        Restore internal state from object returned by getstate().  # 儲存getstate()取得並返回的對象。        shuffle(x, random=None) method of Random instance  # 隨機執行個體的方法        Shuffle list x in place, and return None.  # 打亂列表x並返回None  x為必傳列表參數                Optional argument random is a 0-argument function returning a  # 選擇性參數為一個不需參數的返回0-1之間浮點數的函數        random float in [0.0, 1.0); if it is the default None, the     # 如果選擇性參數為預設的None,則它會使用random.random函數方法        standard random.random will be used.        如果你有一個更好的隨機數產生器,或者說你有一個適合自己應用的隨機數發生器,那麼你就可以使用它而不是使用系統預設那個。        triangular(low=0.0, high=1.0, mode=None) method of Random instance  # 隨機執行個體的方法        Triangular distribution.  # 三角分布(triangular distribution),亦稱辛普森分布或三角形分布                Continuous distribution bounded by given lower and upper limits,  # 三角形分布是低限為low、上限為high、眾數預設為兩者平均數的連續機率分布。        and having a given mode value in-between.                http://en.wikipedia.org/wiki/Triangular_distribution        uniform(a, b) method of Random instance  # 隨機執行個體的方法        Get a random number in the range [a, b) or [a, b] depending on rounding.  # 從a與b之間取一個隨機數,一定可以取到a,b能否取到看b的舍入        a b兩個參數必須有,可為整型可為浮點型,目前本人知道的取到b的方法的用math.ceil        這個方法返回的是一個隨機數        vonmisesvariate(mu, kappa) method of Random instance  # 隨機執行個體的方法        Circular data distribution.  # 迴圈資料分布或馮·米塞斯分布                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 是位置的度量,它的聚會在0-2*pi之間,kappa是集中度的度量,它必須大於或等於0。        # 如果kappa等於0,這個分布是均勻分布,對於κ很小的情形,分布近似均勻分布,其位置度量在0-2*pi之間        weibullvariate(alpha, beta) method of Random instance        Weibull distribution.  # 韋布爾分布                alpha is the scale parameter and beta is the shape parameter.  # λ>0是比例參數(scale parameter),k>0是形狀參數(shape parameter)        # 在這裡alpha是比例參數,beta是形狀參數        威布爾分布(Weibull distribution),又稱韋伯分布或韋布爾分布,是可靠性分析和壽命檢驗的理論基礎。        威布爾分布:在可靠性工程中被廣泛應用,尤其適用於機電類產品的磨損累計失效的分布形式。由於它可以利用機率值很容易地推斷出它的分布參數,        被廣泛應用於各種壽命實驗的資料處理。  參考:http://blog.csdn.net/zheng_lan_fang/article/details/76684761

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.