Skip to content

Class: UtilsNumber ​

Random number utilities: draw an integer within a range and run percentage-based probability tests.

Remarks ​

Accessible everywhere via k.Utils.Number. All methods are static.

Example ​

ts
// Roll a 6-sided die
const de = k.Utils.Number.random(1, 6);

Constructors ​

Constructor ​

ts
new UtilsNumber(): UtilsNumber;

Returns ​

UtilsNumber

Methods ​

chance() ​

ts
static chance(chance?): boolean;

Flips a coin based on a percentage probability: returns true with a chance % probability.

Parameters ​

chance? ​

number = 50

Probability (in %) that the event occurs. Clamped between 0 and 100.

Returns ​

boolean

true if the event occurs, false otherwise.

Default Value ​

50

Example ​

ts
// 20% chance to spawn a bonus
if (k.Utils.Number.chance(20)) {
    spawnBonus();
}

random() ​

ts
static random(min?, max?): number;

Returns a random integer between min and max, both bounds included.

Parameters ​

min? ​

number = 0

Lower bound, included.

max? ​

number = 1

Upper bound, included.

Returns ​

number

The integer drawn at random within the range.

Default Value ​

0

Default Value ​

1

Example ​

ts
k.Utils.Number.random(1, 100); // an integer between 1 and 100