<>均匀分布U(a,b)均值和方差
·
<>验证U(a,b)的均值是(a+b)/2
""" 2020/2/23 验证U(a,b)的均值是不是(a+b)/2 """ from random import uniform n = int(
input('please enter the number of test: ')) # 试验的次数(越多越趋于真实值) a = float(input(
'please enter the lower bond:: ')) b = float(input('please enter the upper
bond:: ')) sums = 0 for i in range(n): sums += uniform(a,b) print('均值是'+str(
'%.2f'%(sums/n))) print('(a+b)/2的值是:%.2f'%((a+b)/2))
得出均匀分布随机取值所得的数的均值趋近于(a+b)/2
please enter the number of test: 10000 please enter the lower bond: 2 please
enter the upper bond: 10 均值是5.99(a+b)/2的值是:6.00
<>验证U(a,b)的方差是(a-b)**2/12
方差计算方法:
""" 2020/2/27 验证U(a,b)的方差是不是(a-b)**2/12 """ from random import uniform n = int(
input('please enter the number of test: ')) a = float(input('please enter the
lower bond: ')) b = float(input('please enter the lower bond: ')) sums = 0 for i
in range(n): sums += (uniform(a,b)-(a+b)/2)**2 print('方差是'+str('%.2f'%(sums/n)))
print('(a-b)**2/12的值是:%.2f'%(((a-b)**2)/12)) please enter the number of test:
10000 please enter the lower bond: 2 please enter the lower bond: 20 方差是27.17(
a-b)**2/12的值是:27.00