BigIntegers { public static BigInteger[] random(BigInteger from, BigInteger to, Random rnd, int size, int bits) { InputValidation.begin() .when(from == null, "from is null") .when(to == null, "to is null") .when(from != null && to != null && from.compareTo(to) >= 0, "from not less than to") .when(size < 0, "size less than zero") .when(bits < 1, "bits less than one") .validate(); BigInteger range = to.subtract(from); BigInteger bitmax = BigInteger.ONE.shiftLeft(bits); BigInteger[] bigs = new BigInteger[size]; for(int i = 0; i < bigs.length; i++) bigs[i] = new BigInteger(bits, rnd).multiply(range).divide(bitmax).add(from); return bigs; } private  BigIntegers(); static BigInteger[] range(int from, int to); static BigInteger[] random(BigInteger from, BigInteger to, Random rnd, int size, int bits);  }
@Test public void testRandom() { BigInteger[] bigs = BigIntegers.random( BigInteger.ZERO, BigInteger.TEN, new Random(), 1000, 4); Set<BigInteger> numerals = new HashSet<BigInteger>(Arrays.asList(bigs)); Set<BigInteger> expected = new HashSet<BigInteger>(Arrays.asList( BigInteger.valueOf(0), BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9) )); Assert.assertEquals(expected, numerals); }