Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4483836

Random generation of BigInteger primes may be wrong (or docs may be wrong)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P5 P5
    • None
    • 1.3.1
    • core-libs
    • generic
    • generic

      Name: bsC130419 Date: 07/24/2001


      [probably 1.1.8 -- but see test results below for 1.3.x and 1.4 beta]

      I'm operating in MacOS 9.1 with Metrowerks and I'm not sure if I've selected
      the right options - inparticular I'm interested in a class from java.math which
      doesn't seem to be listed above - but I think my bug (or feature? see below) is
      possibly platform independent.


      I'm not sure if this is a bug or a feature. In The constructor BigInteger(int
      bitLength, int certainty, Random rnd) claims that it constructs a randomly
      generated positive BigInteger that is probably prime, with the specified
      bitLength.

      Should this mean that the prime is between 0 and 2^bitLength? It appears that
      it is between 2^(bitLength-1) and 2^bitLength as the following program
      demonstrates:

      /**
       * LargePrimes.java
       *
       * Description:
       * @author ad
       * @version
       */

      import java.math.*;
      import java.util.*;

      public class LargePrimes {
      public LargePrimes() {
      }

      static int power(int n)
      {
      int i,j=1;
      for (i=1;i<=n;i++) j *= 2;
      return j;
      }

      // Main entry point
      static public void main(String[] args) {
      int bits = 7;
      int powbits = power(bits);
      int [] tally = new int[powbits];
      int i;
      for (i = 0; i < powbits; i++){tally[i] = 0;}
      Random r = new Random();
      for (i = 1; i <= 10000; i++)
      { BigInteger prime = new BigInteger(bits, 10, r);
      tally[prime.intValue()]++;
      }
      for (i = 0; i < powbits; i++)
      {
      if(tally[i] != 0){System.out.println(" " + i + " " + tally[i]);}
      }
      }

      }

      which gives output:

       67 744
       71 811
       73 744
       79 748
       83 753
       89 830
       97 764
       101 743
       103 765
       107 779
       109 782
       113 742
       127 795

      i.e. no primes less than 64 were generated when a bitlength of 7 was specified.

      This MAY be what's wanted, but I think the specification of the constructor
      is ambiguous.

      ----------

      12 Jul 2001, eval1127@eng -- test results:

      1.3.0:
      --------

       67 730
       71 807
       73 761
       79 757
       83 779
       89 739
       97 790
       101 755
       103 749
       107 789
       109 742
       113 811
       127 791

      1.4 beta (build 65):
      --------------------------
       67 759
       71 783
       73 771
       79 750
       83 762
       89 813
       97 752
       101 790
       103 767
       107 770
       109 761
       113 745
       127 777
      (Review ID: 127838)
      ======================================================================

            Unassigned Unassigned
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: