# 生成指数分布随机变量import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import csv
classRandom(object):def__init__(self, x=None):# 初始函数,x为初始种子,缺省为时间值if x isNone:import time
self.seed =int(time.time())else:
self.seed =int(x)
self._seed = self.seed
defrandom_rand(self):'''
线性同余产生0~1之间的随机数
X(k) = [a * X(k-1) + c] mod m
'''# 以下参数值参照GCC编译器
m =2**32
a =1103515245
c =12345
self._seed =(a * self._seed + c)% m
return self._seed /float(m -1)
JT_random = Random(1)defexponential_rand(lam):if lam <=0:# 检验参数λ的合法性return-1
u = JT_random.random_rand()# 生成随机数return(-1.0/ lam)* np.log(u)# 根据实现原理的公式 计算指数分布随机变量
a =[]
b={}for i inrange(0,1000):
c =int(JT_random.random_rand()+1)
k= exponential_rand(c)*10
a.append(int(k))for i in a:if i in b.keys():
b[i]+=1continue
b[i]=1n
for i,j in b.items():print(i," ",j)withopen("kkk.csv","a", newline='')as f:
writer = csv.writer(f)
writer.writerow(b.keys())
writer.writerow(b.values())