Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
import sys
from random import randint
from math import ceil
def run_simulation(N, sims):
threshold = int(0.5 + 2. * N / 3)
print('{} / {}'.format(threshold, N))
results = []
for i in range(sims):
infections = [False] * N
infections[0] = True
n = 0
while True:
num_infected = sum(infections)
if num_infected >= threshold:
results.append(n)
break
next_infections = infections.copy()
for node in range(N):
if infections[node]:
peer = randint(0, N - 2)
if peer >= node:
peer += 1
next_infections[peer] = True
infections = next_infections
n += 1
results = np.array(results)
print('Results from {} sims'.format(sims))
print('Mean: {}'.format(results.mean()))
print('Min: {}'.format(results.min()))
print('Max: {}'.format(results.max()))
return results.mean()
if __name__ == '__main__':
N = int(sys.argv[1]) if len(sys.argv) > 1 else 50
sims = int(sys.argv[2]) if len(sys.argv) > 1 else 100
print('Running {} sims with N = {}'.format(sims, N))
run_simulation(N, sims)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.