Skip to content

Instantly share code, notes, and snippets.

@PhDP
Created September 22, 2013 18:11
Show Gist options
  • Save PhDP/6662332 to your computer and use it in GitHub Desktop.
Save PhDP/6662332 to your computer and use it in GitHub Desktop.
The Well1024 random number generator in C#
namespace Utils
{
class Well
{
private uint seed;
private uint state_n;
private uint[] state;
private uint mat3pos(int t, uint v)
{
return v ^ (v >> t);
}
private uint mat3neg(int t, uint v)
{
return v ^ (v << (-t));
}
public Well(uint seed)
{
this.seed = seed;
state_n = 0;
state = new uint[32];
state[0] = seed & 0xffffffffu;
for (int i = 1; i < 32; ++i)
{
state[i] = (69069u * state[i - 1]) & 0xffffffffu;
}
}
public double sample()
{
uint z1 = state[state_n]^mat3pos(8, state[(state_n + 3u) & 0x0000001fu]);
uint z2 = mat3neg(-19, state[(state_n + 24u) & 0x0000001fu])^(mat3neg(-14, state[(state_n + 10u) & 0x0000001fu]));
state[state_n] = z1 ^ z2;
state[((state_n + 31u) & 0x0000001fu)] = (mat3neg(-11, state[(state_n + 31u) & 0x0000001fu])) ^ mat3neg(-7, z1) ^ mat3neg(-13, z2);
state_n = (state_n + 31u) & 0x0000001fu;
return state[state_n] * 2.32830643653869628906e-10;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment