If provided, this seed will be used to determine the sequence of numbers this RNG produces. You can re-use a seed in the future to get the same sequence of values, which may be useful for world map generation, etc. If unspecified, a random seed will be used.
The current offset; whenever a value is requested from the RNG, this offset increases by one. If you change the offset, values from a different part of the sequence of random numbers can be retrieved.
Hash a string into an integer suitable for use as a seed, using the cyrb53 algorithm.
The string to hash.
An optional seed value that affects the output. If omitted, 0
will be used.
A positive integer.
Generates a random positive integer suitable for use as a seed.
Returns a random boolean based on the specified chance value.
Determines how often the boolean will be true. For instance, chance(1/6)
will return true 1/6th of the time.
true or false.
Example:
const rng = new Random();
const result = rng.chance(1/10); // result is true one tenth of the time
This method advances this RNG's offset in the sequence by 1.
Returns a random boolean. There is an equal chance whether it will be true or false (50/50).
This method advances this RNG's offset in the sequence by 1.
Given the length of an Array-like structure, returns an integer that could be used as an index in that structure.
The length of the Array (or string, or whatever).
A randomly-chosen index; ie. an integer in the range [0, length
- 1].
This method advances this RNG's offset in the sequence by 1.
Get a random number within the specified range.
The lower bound of the range.
The upper bound of the range.
The resulting random number, in the range [lower
, upper
). The number may (and most likely will) have a fractional part.
This method advances this RNG's offset in the sequence by 1.
Changes the seed for this RNG to a different value.
The new seed.
If no seed argument is provided, a random seed will be chosen.
This method does not affect this RNG's offset. To reset the offset,
call .reset()
.
Resets this RNG so that it starts giving values from the beginning of the sequence again.
This method sets this RNG's offset to 0.
Rolls a virtual die and returns the result.
The number of sides the die should have. For example, 6
.
The result of the die roll; an integer in the range [1, dieSides
].
Example:
const rng = new Random();
const result = rng.roll(6); // rolls a d6
This method advances this RNG's offset in the sequence by 1.
Shuffle the contents of an Array in-place, mutating the array.
This method advances this RNG's offset in the sequence by an amount equivalent to the array's length.
The array to shuffle.
Internally, this method uses the Fisher-Yates shuffling algorithm.
If you would like to shuffle a clone of the array instead of mutating it in-place, you can do this:
const myClone = [...myArray];
Random.shuffle(myClone);
Generates a string of the provided length containing random characters.
The length of the string to generate.
An array containing characters that could end up in the result. If unspecified, defaults to an array containing a
through z
, A
through Z
, and 0
through 9
.
The resulting randomly-generated string.
This method advances this RNG's offset in the sequence by an amount
equivalent to the length
parameter.
Using the specified weights for the given values, pick a random value.
An array of objects shaped like { weight: number, value: any }.
Example:
const random = new Random();
const result = random.weightedSample([
{ value: "apple", weight: 1 },
{ value: "potato", weight: 3 },
{ value "banana", weight: 0.5 }
]);
// potato will be picked more commonly than apple, and banana will be
// the rarest.
console.log(result); // either "apple", "potato", or "banana".
You can use any type for the value
properties; as such, you can get
numbers, strings, objects... etc.
This method advances this RNG's offset in the sequence by one.
Returns a random boolean based on the specified chance value.
Determines how often the boolean will be true. For instance, chance(1/6)
will return true 1/6th of the time.
true or false.
Example:
const result = Random.chance(1/10); // result is true one tenth of the time
Returns a random boolean. There is an equal chance whether it will be true or false (50/50).
Given the length of an Array-like structure, returns an integer that could be used as an index in that structure.
The length of the Array (or string, or whatever).
A randomly-chosen index; ie. an integer in the range [0, length
- 1].
Get a random number within the specified range.
The lower bound of the range.
The upper bound of the range.
The resulting random number, in the range [lower
, upper
). The number may (and most likely will) have a fractional part.
Rolls a virtual die and returns the result.
The number of sides the die should have. For example, 6
.
The result of the die roll; an integer in the range [1, dieSides
].
Example:
const result = Random.roll(6); // rolls a d6
Shuffle the contents of an Array in-place, mutating the array.
The array to shuffle.
Internally, this method uses the Fisher-Yates shuffling algorithm.
If you would like to shuffle a clone of the array instead of mutating it in-place, you can do this:
const myClone = [...myArray];
Random.shuffle(myClone);
Generates a string of the provided length containing random characters.
The length of the string to generate.
An array containing characters that could end up in the result. If unspecified, defaults to an array containing a
through z
, A
through Z
, and 0
through 9
.
The resulting randomly-generated string.
Using the specified weights for the given values, pick a random value.
An array of objects shaped like { weight: number, value: any }.
Example:
const result = Random.weightedSample([
{ value: "apple", weight: 1 },
{ value: "potato", weight: 3 },
{ value "banana", weight: 0.5 }
]);
// potato will be picked more commonly than apple, and banana will be
// the rarest.
console.log(result); // either "apple", "potato", or "banana".
You can use any type for the value
properties; as such, you can get
numbers, strings, objects... etc.
This method advances this RNG's offset in the sequence by one.
Constructs a seedable random value generator.