Skip to content

Instantly share code, notes, and snippets.

@yonatang
Created March 29, 2020 04:02
Show Gist options
  • Save yonatang/dd3ba8d9c83186e9ac5e8947c19cc73c to your computer and use it in GitHub Desktop.
Save yonatang/dd3ba8d9c83186e9ac5e8947c19cc73c to your computer and use it in GitHub Desktop.
SickSimulation
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.math.RandomUtils;
public class Simulation {
static int N = 8000000;
static int ROUNDS = 24;
static int INITIAL_SICKS = 5000;
static int INITIAL_TESTS = 100;
static boolean pop[] = new boolean[N];
private static void infect(int number) {
for (int i = 0; i < number; i++) {
int idx = RandomUtils.nextInt(N);
int j = 0;
while (j < N) {
if (!pop[(idx + j) % N]) {
pop[(idx + j) % N] = true;
break;
}
j++;
}
}
}
private static int getNewCheck(Set<Integer> checked) {
while (true) {
int idx = RandomUtils.nextInt(N);
if (!checked.contains(idx)) {
checked.add(idx);
return idx;
}
}
}
private static int sample(int number) {
int sick = 0;
number = Math.min(number, N);
Set<Integer> checked = new HashSet<>();
for (int i = 0; i < number; i++) {
if (pop[getNewCheck(checked)]) {
sick++;
}
}
return sick;
}
public static void main(String[] args) {
int sicksToday = INITIAL_SICKS;
int testsToday = INITIAL_TESTS;
int idx = RandomUtils.nextInt(N);
pop[idx] = true;
List<Integer> results = new ArrayList<>();
for (int i = 0; i < ROUNDS; i++) {
sicksToday *= 1.2;
testsToday *= 1.1;
// testsToday = sicksToday/2;
infect(sicksToday);
int numToCheck = Math.min(testsToday, N);
int foundToday = sample(numToCheck);
int percents = (int) (((double) foundToday / (double) numToCheck) * 100.0);
results.add(percents);
System.out.println("Number of tests: " + numToCheck + ", number of actual sick: " + sicksToday
+ ", found today: " + foundToday + ", %"
+ percents);
}
System.out.println();
System.out.println("Percents:");
for (Integer result : results) {
System.out.println(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment