// CS 1538 Fall 2009 // See comments in Rand1.java handout out previously. // I added methods here to allow output as doubles, both in 16-bits // and in 32-bits public class Rand2 { private final long mask = (1L << 32) - 1; // this is "m" = 2^32 private final long multiplier = 1194211693L; // this is "a" private final long adder = 12345L; // this is "c" private long randSeed; public Rand2() { randSeed = System.currentTimeMillis(); } public Rand2(long s) { randSeed = s; } public int nextShort(int n) { randSeed = (multiplier * randSeed + adder) & mask; int shortAns = Math.abs((int) randSeed >> 16); return (shortAns % n); } public double nextDouble() { randSeed = (multiplier * randSeed + adder) & mask; int shortAns = Math.abs((int) randSeed >> 16); return ((double)shortAns/65536); } public double nextDouble32() { randSeed = (multiplier * randSeed + adder) & mask; long intAns = Math.abs(randSeed); return ((double)intAns/(mask+1)); } }