Create an instance of Random with a given seed. Instances created with the same seed will output the same sequence of values, as long as each method is called in the same order each time.
String to be hashed and used as an initial state.
Create an instance with a seed string:
let myRandom = new bge.Random("my cool seed");
let value = myRandom.int(10, 20);
Seed string used when creating this instance.
Print the seed of the shared instance:
console.log(bge.random.seed);
Returns true
with the given probability, which is 0.5
by default (50%).
Chance of returning true, between 0
and 1
.
Fair coin flip:
console.log(bge.random.chance() ? "Heads" : "Tails");
Weighted coin flip, with a 75% bias towards heads:
console.log(bge.random.chance(0.75) ? "Heads" : "Tails");
Generates a uniformly-distributed floating-point number between 0
(inclusive) and 1
(exclusive).
Generate a floating-point value between 0
and 1
:
let value = bge.random.float();
Generates a uniformly-distributed floating-point number between 0
(inclusive) and max (exclusive).
The generated number will be less than this value.
Generate a floating-point value between 0
and 10
:
let value = bge.random.float(10);
Generates a uniformly-distributed floating-point number between min (inclusive) and max (exclusive).
The generated number will be at least this value.
The generated number will be less than this value.
Generate a floating-point value between 10
and 20
:
let value = bge.random.float(10, 20);
Generates a uniformly-distributed integer between 0
(inclusive) and max (exclusive).
The generated number will be less than this value.
Generate either 0
, 1
, 2
, 3
or 4
:
let value = bge.random.int(5);
Generates a uniformly-distributed integer between min (inclusive) and max (exclusive).
The generated number will be at least this value.
The generated number will be less than this value.
Generate either 10
, 11
, 12
, 13
or 14
:
let value = bge.random.int(10, 15);
Returns a uniformly selected random item from an array.
Array of items to select from.
An item chosen from the given array.
Pick a random name from a list:
let list = ["Alice", "Bob", "Charlie"];
let name = bge.random.item(list);
Shuffles all items of the given array in-place.
Array of items to shuffle
Shuffle a list of numbers:
let list = [1, 2, 3, 4, 5];
bge.random.shuffle(list);
Shuffles a range of items of the given array in-place.
Array of items to shuffle.
Start of the range to shuffle, from 0.
Exclusive end of the range to shuffle.
Shuffle the middle 3 items of a list with 5 numbers:
let list = [1, 2, 3, 4, 5];
bge.random.shuffle(list, 1, 4);
Generated using TypeDoc
Helper with methods to generate random numbers.
Example
Use the global shared instance:
Create an instance with a seed string: