Open main menu

Sead/Pseudorandom Number Generator

sead::Random is an implementation of a Mersenne Twister pseudorandom number generator.

Contents

Implementation

The class has 4 uint fields.

uint stateOne;
uint stateTwo;
uint stateThree;
uint stateFour;

init()

Calls nn::os::GetSystemTick(), and uses the resulting value as the seed (see below).

init(uint)

Uses the provided uint as the seed.

stateOne = 1812433253 * (seed ^ (seed >> 30)) + 1;
stateTwo = 1812433253 * (stateOne ^ (stateOne >> 30)) + 2;
stateThree = 1812433253 * (stateTwo ^ (stateTwo >> 30)) + 3;
stateFour = 1812433253 * (stateThree ^ (stateThree >> 30)) + 4;

init(uint, uint, uint, uint)

Uses the four provided uints as the initial state.

getU32()

Returns a pseudorandom uint value.

v1 = stateOne ^ (stateOne << 11);
stateOne = stateTwo;
v2 = stateFour;
v3 = v1 ^ (v1 >> 8) ^ v2 ^ (v2 >> 19);
stateTwo = stateThree;
stateThree = v2;
stateFour = v3;

getU64()

Returns a pseudorandom ulong value.

v1 = stateTwo;
v2 = stateOne ^ (stateOne << 11);
v3 = stateFour;
stateOne = stateThree;
stateTwo = v3;
v4 = v2 ^ (v2 >> 8) ^ v3;
v5 = v4 ^ (v3 >> 19);
v6 = v1 ^ (v1 << 11) ^ ((v1 ^ (v1 << 11)) >> 8) ^ v5 ^ (v4 >> 19);
stateThree = v5;
stateFour = v6;
return v6 | (v5 << 32);

getContext(uint*, uint*, uint*, uint*)

Sets the current state values in the provided pointers.

Ports