Random Number Algorithm – Implementation in Java

I think your question actually language-agnostic, and shouldn’t be specifically java. Also, I think it’s very easy to find information on things like this. Look at the wiki pages for Pseudorandom number generator and Random number generation. If you’re looking for something simple (relatively) look at the XORshift RNG

uint32_t xor128(void) { //A C version from wiki
  static uint32_t x = 123456789;
  static uint32_t y = 362436069;
  static uint32_t z = 521288629;
  static uint32_t w = 88675123;
  uint32_t t;

  t = x ^ (x << 11);
  x = y; y = z; z = w;
  return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}

Or in Java:

public class IsNotFour {
    int x = 123456789;
    int y = 362436069;
    int z = 521288629;
    int w = 88675123;

    int xor128() { // A converted C version from wiki
        int t = x ^ (x << 11);
        x = y;
        y = z;
        z = w;
        w = w ^ (w >>> 19) ^ (t ^ (t >>> 8));
        return w; 
    }
}

Note that x, y, z and w together make the seed.

Leave a Comment