Skip to content

Function: chance()

Overload 1

chance(probability): boolean

Defined in: random.ts:128

Evaluates a probability and returns true or false accordingly.

Parameters

probability

number

A value between 0 and 1 representing the probability (eg. 0.75 for 75%).

Returns

boolean

true if the chance is met, false otherwise.

Example

ts
chance(0.75)   // true 75% of the time
chance(0)      // always false
chance(1)      // always true
chance(-1)     // always false, chance is evaluated between `0` and `1`
chance(75)     // always true, values higher than `1` always produce a positive output

Overload 2

chance(favorable, total): boolean

Defined in: random.ts:142

Evaluates a probability expressed as a fraction and returns true or false accordingly.

Parameters

favorable

number

The number of favorable outcomes.

total

number

The total number of outcomes.

Returns

boolean

true if the chance is met, false otherwise.

Example

ts
chance(1, 3)     // true 1 time out of 3
chance(3, 4)     // true 3 times out of 4
chance(0.75, 1)  // same as chance(0.75) with a single parameter
chance(0, 0)     // always false
chance(20, 0)    // always false