mazey
    Preparing search index...

    Function isHit

    • 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);

      Output:

      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);

      Output:

      499994 500006
      

      Parameters

      • rate: number

        Probability expressed as a value from 0 to 1.

      Returns boolean

      Whether the random value is less than the probability.