Skip to content

Instantly share code, notes, and snippets.

@Ctrlmonster
Last active November 19, 2024 19:07
Show Gist options
  • Save Ctrlmonster/66ce786a0853bdf5965c973678b75aa1 to your computer and use it in GitHub Desktop.
Save Ctrlmonster/66ce786a0853bdf5965c973678b75aa1 to your computer and use it in GitHub Desktop.
// 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