Skip to content

7. Randomness

CookieShade edited this page Jul 16, 2016 · 2 revisions

Random number generation

Random number generation for basic uses.

Note: Technically pseudo-random, not truly random. Get your own library if you care.

BETA.randNum(a, b)

Generates a random real number in the range [a, b). Roughly evenly distributed.

var num = BETA.randNum(-2.5, 2); // -2.5 ≤ x < 2

BETA.randInt(a, b)

Generates a random integer in the range [a, b]. Roughly evenly distributed. Returns NaN if there is no integer in [a, b].

var int = BETA.randInt(-2.5, 2); // -2 ≤ x ≤ 2

BETA.randElement(arr)

Returns a randomly selected element in an array. Each element is roughly equally likely to be selected.

var el = BETA.randElement(["ghost", "skeleton", "zombie", "internet explorer"]) // Returns something terrifying