1. 程式人生 > >python學習筆記:泊松分佈和負指數分佈隨機數的python實現

python學習筆記:泊松分佈和負指數分佈隨機數的python實現

不能算是純原創吧,算半個轉載好了,但保證一定能用!

首先匯入模組

import math
import random


泊松分佈

def poisson(L):
    """
    poisson distribution
    return a integer random number, L is the mean value
    """
    p = 1.0
    k = 0
    e = math.exp(-L)
    while p >= e:
        u = random.random()  #generate a random floating point number in the range [0.0, 1.0)
        p *= u
        k += 1
    return k-1

負指數分佈
def expntl(L):
    """
    negative exponential distribution
    return a double random number, L is the mean value
    """
    u = random.random()
    return -L * math.log(u)

大可以迴圈個N次求平均來測試正確性!