Skip to content

@saehrimnir/druidjs / Randomizer

Class: Randomizer ​

Defined in: util/randomizer.js:45

Seeded pseudo-random number generator.

Implements sfc32 (Small Fast Counting, Doty-Humphrey), a 128-bit-state counter-based generator that passes TestU01 BigCrush. It is preferred here over the more familiar Mersenne Twister, which needs a 2.5 KB state, is markedly slower to seed, and whose GF(2)-linear output fails BigCrush's matrix-rank and linear-complexity tests.

Every operation is 32-bit integer arithmetic (^ << >>> + and Math.imul), all of which ECMAScript specifies exactly. The stream is therefore identical on every engine for a given seed — unlike floating point transcendentals, which are only "implementation-approximated". See DR for what that does and does not guarantee about the output of an algorithm.

See ​

where sfc32 originates

Example ​

ts
const R = new Randomizer(1212);
R.random;      // float in [0, 1)
R.random_int;  // uint32

Constructors ​

Constructor ​

ts
new Randomizer(_seed?: number): Randomizer;

Defined in: util/randomizer.js:63

Parameters ​

ParameterTypeDescription
_seed?numberThe seed for the random number generator. If _seed == null then the actual time gets used as seed. Default is new Date().getTime()

Returns ​

Randomizer

Properties ​

PropertyTypeDefault valueDefined in
_anumber0util/randomizer.js:47
_bnumber0util/randomizer.js:49
_cnumber0util/randomizer.js:51
_dnumber0util/randomizer.js:53
_seednumberundefinedutil/randomizer.js:55
_valnumber | nullnullutil/randomizer.js:57

Accessors ​

random ​

Get Signature ​

ts
get random(): number;

Defined in: util/randomizer.js:95

Returns a float between 0 and 1.

Returns ​

number

  • A random number between [0, 1]

random_int ​

Get Signature ​

ts
get random_int(): number;

Defined in: util/randomizer.js:104

Returns an integer between 0 and MAX_INTEGER.

Returns ​

number

  • A random integer.

seed ​

Get Signature ​

ts
get seed(): number;

Defined in: util/randomizer.js:86

Returns the seed of the random number generator.

Returns ​

number

  • The seed.

Set Signature ​

ts
set seed(_seed: number): void;

Defined in: util/randomizer.js:69

Parameters ​
ParameterType
_seednumber
Returns ​

void

Methods ​

choice() ​

ts
choice<T>(A: T[], n: number): T[];

Defined in: util/randomizer.js:157

Returns n samples drawn from A without replacement.

Uses a partial Fisher-Yates shuffle over a scratch index array, which runs in O(n): A itself is never touched, and exactly n random values are consumed. Removing each picked index with Array.prototype.splice instead would shift the tail on every draw and make the call O(n²) — measurably worse from a few hundred elements upward, and 27× slower at n = 4000.

Type Parameters ​

Type ParameterDescription
TReturns samples from an input Matrix or Array.

Parameters ​

ParameterTypeDescription
AT[]The input Matrix or Array.
nnumberThe number of samples.

Returns ​

T[]

A random selection form A of n samples.


gauss_random() ​

ts
gauss_random(): number;

Defined in: util/randomizer.js:127

Returns a normally distributed number with mean 0 and standard deviation 1.

Uses the Marsaglia polar method, which yields two values per iteration; the spare is cached and returned by the following call.

Returns ​

number

A standard normal variate.


choice() ​

ts
static choice<T>(
   A: T[], 
   n: number, 
   seed?: number): T[];

Defined in: util/randomizer.js:189

Type Parameters ​

Type ParameterDescription
TReturns samples from an input Matrix or Array.

Parameters ​

ParameterTypeDefault valueDescription
AT[]undefinedThe input Matrix or Array.
nnumberundefinedThe number of samples.
seednumber1212The seed for the random number generator.

Returns ​

T[]

  • A random selection form A of n samples.