General featured interface to a pseudorandom number generator.

Static methods

staticrandomSystemInt():Int

Returns a (non-secure) random integer from the system.

This method can be used construct and obtain a seed. Unlike regular Sys.random, all bits are usable.

For high-quality (cryptographic) random numbers, see the trandom library (https://lib.haxe.org/p/trandom).

Constructor

new(?seed:Int64, ?generator:GeneratorInterface)

Parameters:

seed

Optional 64-bit seed to initialize the state of the generator. If not given, a seed from randomSystemInt will be used.

generator

Optional generator instance. If not given, a xorshift128+ generator is used.

Variables

seed:Int64

state:Bytes

read onlyusesAllBits:Bool

Methods

choice<T>(array:Array<T>):T

Returns an element from the given array.

nextFullInt():Int

Returns an integer where all bits are uniformly distributed.

nextInt():Int

random():Float

Returns a floating point number in the range [0, 1), That is, a number greater or equal to 0 and less than 1.

The distribution is approximately uniform.

randomInt(lower:Int, upper:Int):Int

Returns an integer within the given range [`lower`, `upper`]. That is, a number within lower inclusive and upper inclusive.

This method uses random() which the distribution approximately uniform. For large ranges greater than 2^53, the distribution will be noticeably biased.

setBytesSeed(seed:Bytes):Void

Derives and sets a seed using the given bytes.

This method works by using the SHA-1 hashing algorithm. A 64-bit integer is obtained from the first 64 bits of the digest with the order of bytes in little endian encoding.

setStringSeed(seed:String):Void

Derives and sets a seed using the given string.

This method encodes the string (supposedly UTF-8 all on targets) and derives a seed using setBytesSeed.

shuffle<T>(array:Array<T>):Void

Shuffles the elements, in-place, in the given array.

The shuffle algorithm is modern Fisher-Yates. The underlying number generator limits the number of permutations possible. This means that using this method alone to shuffle a deck of 52 cards will not result in all possible outcomes.

uniform(lower:Float, upper:Float):Float

Returns a uniformly distributed floating point number within the given range [lower, upper). That is, a number within lower inclusive and upper exclusive.

Note when lower or upper approach precision limits, the returned number may equal upper due to rounding.