DrngImpl implements Drng { @Override public long nextLong(long limit) { if (limit < 1) { throw new IllegalArgumentException("Limit must be strictly positive, but is: " + limit); } int bitSize = (Long.SIZE - Long.numberOfLeadingZeros(limit - 1)); byte[] bytes = getBytes(bitSize); long result = Byte.toUnsignedLong(bytes[0]); for (int i = 1; i < bytes.length; i++) { result <<= 8; result ^= Byte.toUnsignedLong(bytes[i]); } return result < limit ? result : nextLong(limit); } DrngImpl(Drbg drbg); @Override int nextInt(int limit); @Override long nextLong(long limit); @Override BigInteger nextBigInteger(BigInteger limit); @Override boolean nextBit();  }
@Test public void testNextLong() { long limit = Integer.MAX_VALUE + 1000L; for (int i = 0; i < 10; i++) { long r = drng.nextLong(limit); assertInRange(r, 0, limit); } }
@Test public void testNextLongByteSize() { int limit = 256; for (int i = 0; i < 10; i++) { long r = drng.nextLong(limit); assertInRange(r, 0, limit); } }
@Test public void testNextLongMaxSize() { long limit = Long.MAX_VALUE; for (int i = 0; i < 10; i++) { long r = drng.nextLong(limit); assertInRange(r, 0, limit); } }
@Test (expected = IllegalArgumentException.class) public void testNextLongNegativeSize() { int limit = -1; for (int i = 0; i < 10; i++) { long r = drng.nextLong(limit); assertInRange(r, 0, limit); } }
@Test (expected = IllegalArgumentException.class) public void testNextLongZeroSize() { int limit = 0; for (int i = 0; i < 10; i++) { long r = drng.nextLong(limit); assertInRange(r, 0, limit); } }