3 min read

Source: Roblox Creator Hub · CC BY 4.0 · View source · Code samples: MIT Imported 2026-10-03. Formatting adapted for this site.

Random

The Random data type generates pseudorandom numbers and directions.

Constructors

NameType / ReturnsDescription
Random.newReturns a new pseudorandom generator using an optional seed.

Random.new

Returns a new Random object. If you don't provide the seed parameter, Random uses a seed from an internal entropy source.

If you provide a seed, it should be within the range [-9007199254740991, 9007199254740991], and Random will round it down to the nearest integer. So seeds of 0, 0.99, and math.random() all produce identical generators. If you need to generate a seed and store it for later retrieval, use math.random(max).

Parameters

NameTypeDefaultDescription
seednumberAn optional number that initializes the pseudorandom number generator's internal state, truncated toward zero to a whole number. Should be within the range [-9007199254740991, 9007199254740991]; values outside this range are clamped to 0.

Code samples: View on Creator Hub (generating-random-seed).

Methods

NameType / ReturnsDescription
Random:NextIntegernumberReturns a pseudorandom integer uniformly distributed over [min, max].
Random:NextNumbernumberReturns a pseudorandom number uniformly distributed over [0, 1].
Random:NextNumbernumberReturns a pseudorandom number uniformly distributed over [min, max].
Random:Shuffle()Uniformly shuffles a table in-place.
Random:NextUnitVectorVector3Returns a unit vector with a pseudorandom direction.
Random:CloneRandomReturns a new Random object with the same state as the original.

Random:NextInteger

Returns a pseudorandom integer uniformly distributed over [min, max], inclusive of both endpoints. If min is greater than max, the two values are swapped so the call still succeeds. Both arguments are truncated toward zero to whole numbers before sampling.

Parameters

NameTypeDefaultDescription
minnumberThe minimum integer value of the range (inclusive). Truncated toward zero to a whole number.
maxnumberThe maximum integer value of the range (inclusive). Truncated toward zero to a whole number.

Returns

TypeDescription
number

Random:NextNumber

Returns a uniform pseudorandom real number in the range of 0 to 1, inclusive.

Returns

TypeDescription
numberA pseudorandom number uniformly distributed over [0, 1].

Random:NextNumber

Returns a uniform pseudorandom real number in the range of min to max, inclusive.

Parameters

NameTypeDefaultDescription
minnumberThe minimum value of the range (inclusive).
maxnumberThe maximum value of the range (inclusive).

Returns

TypeDescription
numberA pseudorandom number uniformly distributed over [min, max].

Random:Shuffle

Uniformly shuffles the array part of tb in-place using NextInteger to pick indices. If there are any nil "holes" in the array part of the table, Shuffle throws an error, since shuffling could change the length.

The hash part of tb is ignored. No metamethods of tb are invoked.

The shuffle is defined to be a Fisher-Yates shuffle so the number of NextInteger calls is guaranteed to be consistent between engine versions for a given size of table.

Parameters

NameTypeDefaultDescription
tbtableThe table whose array part is shuffled in-place.

Returns

TypeDescription
()

Random:NextUnitVector

Returns a unit vector with a pseudorandom direction. Vectors returned from this function are uniformly distributed over the unit sphere.

Returns

TypeDescription
Vector3A unit vector with a pseudorandom direction.

Random:Clone

Returns a new Random object whose internal state is copied from the original. After cloning, both the original and the clone produce the same sequence of pseudorandom values going forward, but they advance independently — consuming a value from one does not affect the other.

Returns

TypeDescription
RandomA new Random object with the same internal state as the original.