Skip to content

Instantly share code, notes, and snippets.

@morbidcamel101
Created January 29, 2020 19:12
Show Gist options
  • Save morbidcamel101/62477623c4d2e04f68c2615d0dcc386b to your computer and use it in GitHub Desktop.
Save morbidcamel101/62477623c4d2e04f68c2615d0dcc386b to your computer and use it in GitHub Desktop.
Simple random number generator. We don't need great randomness, we just need a little and for it to be fast.
internal struct FastRandom // xorshift prng
{
private uint _w, _x, _y, _z;
public FastRandom(int seed)
{
_x = (uint)seed;
_w = 88675123;
_y = 362436069;
_z = 521288629;
}
public int Next(int maxValue)
{
Debug.Assert(maxValue > 0);
uint t = _x ^ (_x << 11);
_x = _y; _y = _z; _z = _w;
_w = _w ^ (_w >> 19) ^ (t ^ (t >> 8));
return (int)(_w % (uint)maxValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment