Hand-written by Joseph San Nicolas, Roblox Developer
Random
Constructors
| Name | Description |
| new(seed: number?) | Creates a new pseudorandom number generator. If a seed is provided it is truncated to an integer within the range -9,007,199,254,740,991 to 9,007,199,254,740,991 and the generator becomes deterministic, always producing the same sequence for that seed. If no seed is given, one is drawn from an internal entropy source instead. |
Code Snippet
local rng = Random.new(12345)
local roll = rng:NextInteger(1, 6)
local chance = rng:NextNumber()
local direction = rng:NextUnitVector()
print("Rolled a", roll, "with a roll chance of", chance, "facing", direction)
Methods
| Name | Parameters | Returns | Description |
| NextInteger(min: number,max: number):number | Min: number, Max: number | number | Returns a pseudorandom integer uniformly distributed over the range [min, max], inclusive of both endpoints. |
| NextNumber():number | Void | number | Returns a uniform pseudorandom real number in the range of 0 to 1, inclusive. |
| NextNumber(min: number,max: number):number | Min: number, Max: number | number | Returns a uniform pseudorandom real number in the range of min to max, inclusive. |
| NextUnitVector():Vector3 | Void | Vector3 | Returns a unit vector with a pseudorandom direction. Vectors returned by this function are uniformly distributed over the unit sphere. |
| Shuffle(tb: table) | Tb: table | Void | Uniformly shuffles the array part of the given table in-place, using the Fisher-Yates algorithm. Throws an error if the table's array portion contains nil holes. |
| Clone():Random | Void | Random | Returns a new Random object whose internal state is copied from the original. After cloning, the original and the clone advance independently and will produce different results. |