Add probability helper functions

This commit is contained in:
Xevion
2022-12-18 00:17:39 -06:00
parent 5f252cd814
commit 5e525a9000
2 changed files with 14 additions and 0 deletions

View File

@@ -3,4 +3,8 @@ export const range = (start: number, stop: number, step = 1) =>
export function classNames(...classes: (string | null | undefined)[]) {
return classes.filter(Boolean).join(" ");
}
export function sum(a: number, b: number): number {
return a + b;
}

10
src/utils/math.ts Normal file
View File

@@ -0,0 +1,10 @@
import {range, sum} from "@/utils/helpers";
export function failureProbability(boxCount: number, attempts: number): number {
const upperRange = boxCount - attempts;
return range(attempts + 1, attempts + upperRange).map(n => 1.0 / n).reduce(sum);
}
export function successProbability(boxCount: number, attempts: number) {
return 1.0 - failureProbability(boxCount, attempts);
}