Return whether a random value falls within the given probability.
Usage:
import { isHit } from "mazey";const ret = isHit(0.5); // A 50% chance of returning true.console.log(ret); Copy
import { isHit } from "mazey";const ret = isHit(0.5); // A 50% chance of returning true.console.log(ret);
Output:
true Copy
true
Example: Test the precision.
let trueCount = 0;let falseCount = 0;new Array(1000000).fill(0).forEach(() => { if (isHit(0.5)) { trueCount++; } else { falseCount++; }});console.log(trueCount, falseCount); Copy
let trueCount = 0;let falseCount = 0;new Array(1000000).fill(0).forEach(() => { if (isHit(0.5)) { trueCount++; } else { falseCount++; }});console.log(trueCount, falseCount);
499994 500006 Copy
499994 500006
Probability expressed as a value from 0 to 1.
Whether the random value is less than the probability.
Return whether a random value falls within the given probability.
Usage:
Output:
Example: Test the precision.
Output: