在我的python代码中有一些地方,我有一个人口,x每个人都有可能发生一些事情,但我只需要受影响的人数.
amount = 0
population = 500
chance = 0.05
for p in range(population):
if random.random() < chance:
amount += 1
Run Code Online (Sandbox Code Playgroud)
我的直觉告诉我,除了调用random.random()500次之外,必须有一种不那么强力的方法.我不知道的一些数学或统计术语或功能.
amount = population * chance * random.random()
Run Code Online (Sandbox Code Playgroud)
根据我的需要变化太多了.