Skip to content

Instantly share code, notes, and snippets.

@Dicee
Last active February 9, 2023 18:40
Show Gist options
  • Save Dicee/e12c1d298993508cef033d5f397cf22c to your computer and use it in GitHub Desktop.
Save Dicee/e12c1d298993508cef033d5f397cf22c to your computer and use it in GitHub Desktop.
Ratio of sums versus average of ratios
public static void main(String[] args) {
Random rd = new Random();
int totalInputs = 0;
int totalOutputs = 0;
List<Double> rates = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
int inputs = 1 + rd.nextInt(100_000);
int outputs = /*rd.nextInt(inputs);*/ Math.min(rd.nextInt(10_000), inputs); // 0.9*5000 + 0.1*5000 = 5000
totalInputs += inputs;
totalOutputs += outputs;
double rate = 100 * (double) outputs / inputs; // 0.9 *0.1 + 0.1 = 0.19
rates.add(rate);
System.out.printf("%d / %d = %f%n", outputs, inputs, rate);
}
double avgSuccess = rates.stream().mapToDouble(Double::doubleValue).average().getAsDouble();
System.out.printf("%f vs %f", 100 * (double) totalOutputs / totalInputs, avgSuccess);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment