Skip to content

Instantly share code, notes, and snippets.

@mgraczyk
Last active June 27, 2018 22:39
Show Gist options
  • Save mgraczyk/09e683827db2b8e1452fd14e30a2c521 to your computer and use it in GitHub Desktop.
Save mgraczyk/09e683827db2b8e1452fd14e30a2c521 to your computer and use it in GitHub Desktop.
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