---
name: Random
last_updated: 2026-06-10T23:09:12Z
type: datatype
summary: "Generates pseudorandom numbers and directions."
---

# Random

Generates pseudorandom numbers and directions.

**Type:** datatype

## Description

The [Random](/docs/reference/engine/datatypes/Random.md) data type generates pseudorandom numbers and directions.

## Constructors

### Random.new

**Signature:** `Random.new(seed: number)`

Returns a new [Random](/docs/reference/engine/datatypes/Random.md) object. If you don't provide the seed
parameter, [Random](/docs/reference/engine/datatypes/Random.md) uses a seed from an internal entropy source.

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

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `seed` | `number` |  |  |

**Datatype.Random**

Generates a pseudorandom seed and uses it to create a new [Random](/docs/reference/engine/datatypes/Random.md)
generator.

```lua
local max = 2147483647 -- use a large integer
local seed = math.random(max)
local generator = Random.new(seed)
```

## Methods

### Random:NextInteger

**Signature:** `Random:NextInteger(min: number, max: number): number`

Returns a pseudorandom integer uniformly distributed over `[min, max]`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `min` | `number` |  |  |
| `max` | `number` |  |  |

**Returns:** `number`

### Random:NextNumber

**Signature:** `Random:NextNumber(): number`

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

**Returns:** `number`

### Random:NextNumber

**Signature:** `Random:NextNumber(min: number, max: number): number`

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

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `min` | `number` |  |  |
| `max` | `number` |  |  |

**Returns:** `number`

### Random:Shuffle

**Signature:** `Random:Shuffle(tb: table): ()`

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:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `tb` | `table` |  |  |

**Returns:** `()`

### Random:NextUnitVector

**Signature:** `Random:NextUnitVector(): Vector3`

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

**Returns:** `Vector3` — A unit vector with a pseudorandom direction.

### Random:Clone

**Signature:** `Random:Clone(): Random`

Returns a new Random object with the same state as the original.

**Returns:** `Random`