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次求平均来测试正确性!