Last active
February 9, 2023 18:40
-
-
Save Dicee/e12c1d298993508cef033d5f397cf22c to your computer and use it in GitHub Desktop.
Ratio of sums versus average of ratios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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