Subclassing java.util.Random

In our discussion of the XORShift generator, we mentioned that we could subclass java.lang.Random to use a different generator.

There is essentially one method that we need to override: the protected method next(), which other methods will call to ask for a certain number of random bits. Here is a naive version, which we'll actually need to correct slightly in a moment:

public class XORShiftRandom extends Random {
  private long seed = System.nanoTime();

  public XORShiftRandom() {
  }
  protected int next(int nbits) {
    // N.B. Not thread-safe!
    long x = this.seed;
    x ^= (x << 21);
    x ^= (x >>> 35);
    x ^= (x << 4);
    this.seed = x;
    x &= ((1L << nbits) -1);
    return (int) x;
  }
}

In principle, we don't actually care about nbits: the XORShift algorithm produces numbers where we can take all bits as equally random. However, it turns out that other methods of Random rely on exactly the right number of random bits being supplied, and other bits being zero. (In my opinion, the documentation of the next() method is misleading on this point.) So, the line x &= ((1L << nbits) -1) masks off any unwanted bits.

A few details we've glossed over:


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.