我正在模拟将999个硬币翻转1000次,并绘制样本均值的分布,这可能需要很长时间(大约21秒)。有一个更好的方法吗?例如,一种更快的循环运行方式。向量化会有用吗?
import datetime
import numpy as np
sample_mean_dis = []
start_time = datetime.datetime.now()
# to draw a distribution of sample mean
for i in range(1000):
if not (i%100):
print('iterate: ', i)
sums_1000coins = []
# simulate 1k repetition of experiment_1
# and consider this opertation as a sample
# and compute the sample mean
for i in range(1000):
# this is simulating experiment_1 which flip 999 coins
# and sum heads
coins = np.random.randint(2, size=999)
sums_1000coins.append(np.sum(1 == coins))
sample_mean_dis.append(np.mean(sums_1000coins))
end_time = datetime.datetime.now()
elapsedTime = end_time - start_time
print("Elapsed time: %d seconds" % (elapsedTime.total_seconds()))
答案 0 :(得分:1)
要掷出999个硬币并查看哪个硬币正面朝上,read 999 bits of random data(有点像硬币可能是Sub Combine_workbooks()
Path = "C:\Users\PChen\Desktop\Test\"
FileName = Dir(Path & "*.xlsx")
Workbooks.Add
Do While FileName <> ""
Workbooks.Open FileName:=Path & FileName, ReadOnly:=True
Call AuthOpenAll
Worksheets("Data").Activate
Worksheets("Data").Copy After:=Workbooks("book1").Worksheets("sheet1")
Workbooks(FileName).Close savechanges:=False
FileName = Dir()
Loop
End Sub
或0
,概率为50/50),然后{ {3}}。
1
以上内容可能会返回接近499.5的数字
要翻转999个硬币1000次,请在import random
bin(random.getrandbits(999)).count("1")
循环中执行上述操作:
for
num_heads = [bin(random.getrandbits(999)).count("1") for _ in range(1000)]
将是1000个整数count how many bits are set to 1
的列表,大约为499.5(999/2)。