Skip to content

Determine whether or not an event should occur based on a given chance.

License

Notifications You must be signed in to change notification settings

voidvoxel/chancellor

Repository files navigation

chancellor

If you need to call a function only part of the time, then this library can make your code more readable.

If you find yourself frquently writing code like the following in your project:

if (Math.random() >= 0.30) {
    doSomething();
}

you can instead write the following using this library:

chanceof(0.30).to(doSomething);

or if you prefer using percentages:

chanceof("30%").to(doSomething);
// or
chanceof.percent(30).to(doSomething);

Installation

npm i chancellor

Importing

import chanceof from "chancellor";

Usage

If statement

if (chanceof(0.42).roll()) {
    console.log("I say hi 42% of the time");
}

or

if (chanceof(0.42).roll()) console.log("I say hi 42% of the time");

While statement

const dice = chanceof(0.7);
while (dice.roll()) console.log("Hello, world!");

Callback

const log = result => console.log(result);

chanceof(0.5, log);

About

Determine whether or not an event should occur based on a given chance.

Resources

License

Stars

Watchers

Forks

Packages

No packages published