Last active
November 19, 2024 19:07
-
-
Save Ctrlmonster/66ce786a0853bdf5965c973678b75aa1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// the returned function will return true at regular intervals p% of the time it gets called. | |
// use instead of (i % N === 0) checks, whenever other frequencies other than (1 / someInteger) are needed. | |
// as accumulated values carry over, we can change the targetd frequency at runtime. | |
const createFrequencyCheck = () => { | |
let sum = 0 | |
return (p: number) => { | |
sum += p | |
if (sum >= 1) { | |
sum = sum - Math.trunc(sum) | |
return true | |
} | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment