Random

Mostrar obsoleto

The Random data type generates pseudorandom numbers and directions.

Resumen

Constructores

  • new(seed : number)

    Returns a new pseudorandom generator using an optional seed.

Métodos

Constructores

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).

Parámetros

seed: number

Muestras de código

Generates a pseudorandom seed and uses it to create a new Random generator.

Datatype.Random

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

Métodos

NextInteger

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

Parámetros

min: number
max: number

Devuelve

NextNumber

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

Devuelve

NextNumber

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

Parámetros

min: number
max: number

Devuelve

Shuffle

void

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.

Parámetros

tb: table

Devuelve

void

NextUnitVector

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

Devuelve

A unit vector with a pseudorandom direction.

Clone

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

Devuelve