import random import matplotlib.pyplot as plt x=[] y=[] def random_pick(some_list, probabilities): random_num = random.uniform(0,1) cumulative_probability = 0.0 for item, item_probability in zip(some_list, probabilities): #遍历 cumulative_probability += item_probability if random_num < cumulative_probability: break return item probabilities=[0.5,0.5] some_list=[1.02,1.74] sum1=sum2=k=0 for i in range(10000): a=random_pick(some_list,probabilities) k+=1 b=some_list.index(a) if b==0: sum1+=a else: sum2+=a if i%10==0: probabilities[0]+=0.01*sum1 probabilities[1]+=0.01*sum2 probabilities[0]=probabilities[0]/sum(probabilities) probabilities[1]=probabilities[1]/sum(probabilities) print(sum1,sum2) sum1=sum2=0 #print(probabilities) y.append(probabilities[0]) x.append(i) print(probabilities) plt.plot(x,y,color='g',linestyle='-')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)