DrngImpl implements Drng { @Override public BigInteger nextBigInteger(BigInteger limit) { if (limit.signum() < 1) { throw new IllegalArgumentException("Limit must be strictly positive, but is: " + limit); } int bitSize = limit.bitLength(); byte[] bytes = getBytes(bitSize); BigInteger result = new BigInteger(1, bytes); return result.compareTo(limit) < 0 ? result : nextBigInteger(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 testNextBigInteger() { BigInteger limit = new BigInteger("1234390583094589083690724905729037458907233245900987120359876321450987345"); for (int i = 0; i < 10; i++) { BigInteger b = drng.nextBigInteger(limit); assertInRange(b, BigInteger.ZERO, limit); } }
@Test public void testNextBigIntegerByteSize() { BigInteger limit = new BigInteger("256"); for (int i = 0; i < 10; i++) { BigInteger b = drng.nextBigInteger(limit); assertInRange(b, BigInteger.ZERO, limit); } }
@Test public void testNextBigIntegerLongSize() { BigInteger limit = new BigInteger("123439058309458908362350987123509834703458923475903745093840934590872340" + "98237469839305077937827359696778362338580384509238907249057290374589072332459" + "00987120359876321450987345"); for (int i = 0; i < 10; i++) { BigInteger b = drng.nextBigInteger(limit); assertInRange(b, BigInteger.ZERO, limit); } }
@Test (expected = IllegalArgumentException.class) public void testNextBigIntegerZeroSize() { BigInteger limit = BigInteger.ZERO; for (int i = 0; i < 10; i++) { BigInteger b = drng.nextBigInteger(limit); assertInRange(b, BigInteger.ZERO, limit); } }
@Test (expected = IllegalArgumentException.class) public void testNextBigIntegerNegativeSize() { BigInteger limit = BigInteger.ONE.negate(); for (int i = 0; i < 10; i++) { BigInteger b = drng.nextBigInteger(limit); assertInRange(b, BigInteger.ZERO, limit); } }