ngneat / falso

All the Fake Data for All Your Real Needs 🙂

Home Page:https://ngneat.github.io/falso/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

randChanceBoolean

PumpedSardines opened this issue · comments

Description

Currently we only have randBoolean, but sometimes you don't want there be 50% chance that the boolean will be true and a 50% chance that the boolean will be false. I therefore propose a function randChanceBoolean where you can specify the chance of the boolean being either true or false.

Proposed solution

You would call the function like this randChanceBoolean({ chanceTrue: 0.76 }) or randChanceBoolean({ chanceFalse: 0.1 }). Both chanceFalse and chanceTrue can't be provided in the same function call.

Here's a simple code example for how this could be implemented:

let base = 1;
let percent = chanceTrue;

while(percent % 1 !== 0) {
    percent *= 10;
    base *= 10;
}

return randNumber({min: 0; max: base}) >= percent;

Alternatives considered

No response

Do you want to create a pull request?

No

I'm aware that my example won't work since floating point numbers doesn't work in that way. It's just an example :^)

Merged in #332