682 /** 683 * Constructs a randomly generated BigInteger, uniformly distributed over 684 * the range 0 to (2<sup>{@code numBits}</sup> - 1), inclusive. 685 * The uniformity of the distribution assumes that a fair source of random 686 * bits is provided in {@code rnd}. Note that this constructor always 687 * constructs a non-negative BigInteger. 688 * 689 * @param numBits maximum bitLength of the new BigInteger. 690 * @param rnd source of randomness to be used in computing the new 691 * BigInteger. 692 * @throws IllegalArgumentException {@code numBits} is negative. 693 * @see #bitLength() 694 */ 695 public BigInteger(int numBits, Random rnd) { 696 this(1, randomBits(numBits, rnd)); 697 } 698 699 private static byte[] randomBits(int numBits, Random rnd) { 700 if (numBits < 0) 701 throw new IllegalArgumentException("numBits must be non-negative"); 702 int numBytes = (int)(((long)numBits+7)/8); // avoid overflow 703 byte[] randomBits = new byte[numBytes]; 704 705 // Generate random bytes and mask out any excess bits 706 if (numBytes > 0) { 707 rnd.nextBytes(randomBits); 708 int excessBits = 8*numBytes - numBits; 709 randomBits[0] &= (1 << (8-excessBits)) - 1; 710 } 711 return randomBits; 712 } 713 714 /** 715 * Constructs a randomly generated positive BigInteger that is probably 716 * prime, with the specified bitLength. 717 * 718 * @apiNote It is recommended that the {@link #probablePrime probablePrime} 719 * method be used in preference to this constructor unless there 720 * is a compelling need to specify a certainty. 721 * | 682 /** 683 * Constructs a randomly generated BigInteger, uniformly distributed over 684 * the range 0 to (2<sup>{@code numBits}</sup> - 1), inclusive. 685 * The uniformity of the distribution assumes that a fair source of random 686 * bits is provided in {@code rnd}. Note that this constructor always 687 * constructs a non-negative BigInteger. 688 * 689 * @param numBits maximum bitLength of the new BigInteger. 690 * @param rnd source of randomness to be used in computing the new 691 * BigInteger. 692 * @throws IllegalArgumentException {@code numBits} is negative. 693 * @see #bitLength() 694 */ 695 public BigInteger(int numBits, Random rnd) { 696 this(1, randomBits(numBits, rnd)); 697 } 698 699 private static byte[] randomBits(int numBits, Random rnd) { 700 if (numBits < 0) 701 throw new IllegalArgumentException("numBits must be non-negative"); 702 703 // It is highly likely that the first, or one of the first few, bytes 704 // generated by Random.nextBytes() will be non-zero thereby making the 705 // distribution highly non-uniform by skewing it to the upper end of 706 // the interval [0, 2^numBits). To improve the uniformity of the 707 // distribution, adjust numBits to a random value in the interval 708 // [0, numBits]. 709 if (numBits < Integer.MAX_VALUE) { 710 final int numBitsOriginal = numBits; 711 numBits = rnd.nextInt(numBits + 1); 712 // Increment the adjusted maximum bit length to help prevent the 713 // very largest numbers from being underrepresented. 714 if (numBits < numBitsOriginal) { 715 numBits++; 716 } 717 } 718 719 int numBytes = (int)(((long)numBits+7)/8); // avoid overflow 720 byte[] randomBits = new byte[numBytes]; 721 722 // Generate random bytes and mask out any excess bits 723 if (numBytes > 0) { 724 rnd.nextBytes(randomBits); 725 int excessBits = 8*numBytes - numBits; 726 randomBits[0] &= (1 << (8-excessBits)) - 1; 727 } 728 return randomBits; 729 } 730 731 /** 732 * Constructs a randomly generated positive BigInteger that is probably 733 * prime, with the specified bitLength. 734 * 735 * @apiNote It is recommended that the {@link #probablePrime probablePrime} 736 * method be used in preference to this constructor unless there 737 * is a compelling need to specify a certainty. 738 * |