Large pseudo random numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    Large pseudo random numbers

    I hope I can get some pointers about multiple precision integers in here.
    My lecturer has given us a task about using java big integer class.

    I use calculator to calculate 2 to the power of 128 and result is this:
    3.4028236692093 846346337460743 177e+38
    What does this 'e+38' mean?

    How multiple precision numbers are being processed in a program?

    Kindly please share some info about this subject with me
    ....Thank You.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by shana07
    I hope I can get some pointers about multiple precision integers in here.
    My lecturer has given us a task about using java big integer class.

    I use calculator to calculate 2 to the power of 128 and result is this:
    3.4028236692093 846346337460743 177e+38
    What does this 'e+38' mean?

    How multiple precision numbers are being processed in a program?

    Kindly please share some info about this subject with me
    ....Thank You.
    The 'e+38' notation means 'times ten raised to the power 38'. Your floating
    point number approximates 2**128 (** being my notation for 'raise to the power).

    You can use BigInteger numbers for this which can calculate, well, very big
    integer numbers; read all about the BigInteger class API docs and pay special
    attention to the pow method.

    kind regards,

    Jos

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by JosAH
      The 'e+38' notation means 'times ten raised to the power 38'. Your floating
      point number approximates 2**128 (** being my notation for 'raise to the power).

      You can use BigInteger numbers for this which can calculate, well, very big
      integer numbers; read all about the BigInteger class API docs and pay special
      attention to the pow method.

      kind regards,

      Jos
      Thank you Jos. Appreciate your effort to reply my question.
      As I have found one website just now, it is also called decimal exponent notation.

      Can I clarify with you that using scien. calc it can only display until 32 decimal digits only, isn't?

      One more question regarding this subject and random number generators.
      Could you please take a look at the below source code, why it produces numbers only differs at the last 4 digits?

      Is it possible for me to get min range of random number =0 from this code,
      it is like from: 0 - 2**49 random numbers?

      I provide 49 numDigits from command prompt ......
      Thank you

      Prime 1 = 772932660120059 030808394745599 610734462482434 1311
      Prime 2 = 772932660120059 030808394745599 610734462482434 1503
      Prime 3 = 772932660120059 030808394745599 610734462482434 1643
      Prime 4 = 772932660120059 030808394745599 610734462482434 1653
      Prime 5 = 772932660120059 030808394745599 610734462482434 1721
      Prime 6 = 772932660120059 030808394745599 610734462482434 1823
      Prime 7 = 772932660120059 030808394745599 610734462482434 1989
      Prime 8 = 772932660120059 030808394745599 610734462482434 2009
      Prime 9 = 772932660120059 030808394745599 610734462482434 2049

      Code:
      import java.math.BigInteger;
      
      public class Primes
       {
        // Note that BigInteger.ZERO was new in JDK 1.2, and 1.1
        // code is being used to support the most servlet engines.
        private static final BigInteger ZERO = new BigInteger("0");
        private static final BigInteger ONE = new BigInteger("1");
        private static final BigInteger TWO = new BigInteger("2");
        
         private static final int ERR_VAL = 100;
        
        public static BigInteger nextPrime(BigInteger start) 
        {
          if (isEven(start))
            start = start.add(ONE);
          else
            start = start.add(TWO);
          if (start.isProbablePrime(ERR_VAL))
            return(start);
          else
            return(nextPrime(start));
        }
      
        private static boolean isEven(BigInteger n) {
          return(n.mod(TWO).equals(ZERO));
        }
      
        private static StringBuffer[] digits =
          { new StringBuffer("0"), new StringBuffer("1"),
            new StringBuffer("2"), new StringBuffer("3"),
            new StringBuffer("4"), new StringBuffer("5"),
            new StringBuffer("6"), new StringBuffer("7"),
            new StringBuffer("8"), new StringBuffer("9") };
      
        private static StringBuffer randomDigit() {
          int index = (int)Math.floor(Math.random() * 10);
          return(digits[index]);
        }
        
        public static BigInteger random(int numDigits) 
        {
          StringBuffer s = new StringBuffer("");
          for(int i=0; i<numDigits; i++) {
            s.append(randomDigit());
          }
          return(new BigInteger(s.toString()));
        }
      
        /** Simple command-line program to test. Enter number
         *  of digits, and it picks a random number of that
         *  length and then prints the first 50 prime numbers
         *  above that.
         */
        
        public static void main(String[] args) 
        {
          int numDigits;
          if (args.length > 0)
            numDigits = Integer.parseInt(args[0]);
          else
            numDigits = 150;
          BigInteger start = random(numDigits);
          for(int i=0; i<10; i++) 
          {
            start = nextPrime(start);
            System.out.println("Prime " + i + " = " + start);
          }
        }
      }

      Comment

      • prometheuzz
        Recognized Expert New Member
        • Apr 2007
        • 197

        #4
        Originally posted by shana07
        One more question regarding this subject and random number generators.
        Could you please take a look at the below source code, why it produces numbers only differs at the last 4 digits?
        Because you create a starting number in your main method and then call the method nextPrime(...) ten times on that starting number getting the next prime after that number. That way you get ten increasing number of (probable) primes, which (if large enough) only differ in their last digits.


        Originally posted by shana07
        Is it possible for me to get min range of random number =0 from this code, it is like from: 0 - 2**49 random numbers?
        If you want a (pseudo) random BigInteger between 0 and 2**49, you could just do this in your main method:
        Code:
        BigInteger start = random(numDigits);
        final BigInteger _2pow49 = new BigInteger("562949953421312"); // == 2**49
        start = start.mod(_2pow49); // start is now between 0 (inclusive) and 2**49 (exclusive)
        The mod(...) method is short for modulo. Details about it can be found here:


        Good luck.

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by prometheuzz
          Because you create a starting number in your main method and then call the method nextPrime(...) ten times on that starting number getting the next prime after that number. That way you get ten increasing number of (probable) primes, which (if large enough) only differ in their last digits.



          If you want a (pseudo) random BigInteger between 0 and 2**49, you could just do this in your main method:
          Code:
          BigInteger start = random(numDigits);
          final BigInteger _2pow49 = new BigInteger("562949953421312"); // == 2**49
          start = start.mod(_2pow49); // start is now between 0 (inclusive) and 2**49 (exclusive)
          The mod(...) method is short for modulo. Details about it can be found here:


          Good luck.
          Thank you. I have replace my main method as what you've suggested. It works but I think it still without start from min: 0. This is the sample result (excerpt) for 1000 random numbers:
          Prime 0 = 140164539147929
          Prime 1 = 140164539147959
          Prime 2 = 140164539148019
          Prime 3 = 140164539148061
          Prime 4 = 140164539148069
          Prime 5 = 140164539148087
          Prime 6 = 140164539148091
          Prime 7 = 140164539148097
          Prime 8 = 140164539148111
          Prime 9 = 140164539148121
          Prime 10 = 140164539148189

          Kindly please advise me ....

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            [QUOTE=shana07]Thank you. I have replace my main method as what you've suggested. It works but I think it still without start from min: 0.

            I don't understand your remark; it doesn't make sense when I read the comments
            just above your main method:
            Code:
            /** Simple command-line program to test. Enter number
               *  of digits, and it picks a random number of that
               *  length and then prints the first 50 prime numbers
               *  above that.
               */
            The program/class does exactly what your comments state (except for the 50
            numbers: you print just 10 of them).

            kind regards,

            Jos

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              [QUOTE=JosAH]
              Originally posted by shana07
              Thank you. I have replace my main method as what you've suggested. It works but I think it still without start from min: 0.

              I don't understand your remark; it doesn't make sense when I read the comments
              just above your main method:
              Code:
              /** Simple command-line program to test. Enter number
                 *  of digits, and it picks a random number of that
                 *  length and then prints the first 50 prime numbers
                 *  above that.
                 */
              The program/class does exactly what your comments state (except for the 50
              numbers: you print just 10 of them).

              kind regards,

              Jos
              Erm so sorry I still don't get it ...as if I provide 'java Primes 5 > check.txt' to prompt, why it printed this: (I just paste for 10 random numbers)
              Prime 0 = 62851
              Prime 1 = 62861
              Prime 2 = 62869
              Prime 3 = 62873
              Prime 4 = 62897
              Prime 5 = 62903
              Prime 6 = 62921
              Prime 7 = 62927
              Prime 8 = 62929
              Prime 9 = 62939

              From my understanding, it prints 5 decimal digit numbers. Correct me if i'm wrong. learning...than k you

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                [QUOTE=shana07]
                Originally posted by JosAH
                Erm so sorry I still don't get it ...as if I provide 'java Primes 5 > check.txt' to prompt, why it printed this: (I just paste for 10 random numbers)
                Prime 0 = 62851
                Prime 1 = 62861
                Prime 2 = 62869
                Prime 3 = 62873
                Prime 4 = 62897
                Prime 5 = 62903
                Prime 6 = 62921
                Prime 7 = 62927
                Prime 8 = 62929
                Prime 9 = 62939

                From my understanding, it prints 5 decimal digit numbers. Correct me if i'm wrong. learning...than k you
                Well the series 62851 ... are five digit numbers aren't they? The first one is a
                pseudo random number. Exactly as your comment said so. You even created
                some methods for that: a random number with 'n' digits. Did you change your
                mind and do you want to start at, e.g. the number 5 now?

                kind regards,

                Jos

                Comment

                • shana07
                  Contributor
                  • Jan 2007
                  • 280

                  #9
                  [QUOTE=JosAH]
                  Originally posted by shana07

                  Well the series 62851 ... are five digit numbers aren't they? The first one is a
                  pseudo random number. Exactly as your comment said so. You even created
                  some methods for that: a random number with 'n' digits. Did you change your
                  mind and do you want to start at, e.g. the number 5 now?

                  kind regards,

                  Jos
                  Yes, I have tried replace the main method as what has been suggested:(beca use my aim is to get 50 random numbers between 0 ~ 2**49).....
                  Code:
                   BigInteger start = random(numDigits);         
                      final BigInteger _2pow49 = new BigInteger("562949953421312"); // == 2**49
                      start = start.mod(_2pow49); // start is now between 0 (inclusive) and 2**49 (exclusive)*/
                  What else missing in the codes ......

                  Comment

                  • shana07
                    Contributor
                    • Jan 2007
                    • 280

                    #10
                    Ok. I should mention that I need to print out 50 random digits between 0 ~ 2**49 - not prime random number. I downloaded this program and trying to adjust to suite with my aims.....kindly please advise me.Thanks

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      [QUOTE=shana07]
                      Originally posted by JosAH
                      Yes, I have tried replace the main method as what has been suggested:(beca use my aim is to get 50 random numbers between 0 ~ 2**49).....
                      Code:
                       BigInteger start = random(numDigits);         
                          final BigInteger _2pow49 = new BigInteger("562949953421312"); // == 2**49
                          start = start.mod(_2pow49); // start is now between 0 (inclusive) and 2**49 (exclusive)*/
                      What else missing in the codes ......
                      But you got 50 numbers (well 10 actually but that's not the problem) in that range.
                      All you said was that these (prime) numbers should be at least 5 digits long.

                      Think over your requirements and try to write them down in a clear and concise
                      way because this thread is going nowhere as it goes now, i.e. you keep on
                      changing your requirements, at least that's what it looks like now and this way
                      none of your questions can be answered properly.

                      kind regards,

                      Jos

                      Comment

                      • shana07
                        Contributor
                        • Jan 2007
                        • 280

                        #12
                        [QUOTE=JosAH]
                        Originally posted by shana07
                        But you got 50 numbers (well 10 actually but that's not the problem) in that range.
                        All you said was that these (prime) numbers should be at least 5 digits long.

                        Think over your requirements and try to write them down in a clear and concise
                        way because this thread is going nowhere as it goes now, i.e. you keep on
                        changing your requirements, at least that's what it looks like now and this way
                        none of your questions can be answered properly.

                        kind regards,

                        Jos
                        Thank Jos.
                        Ok. I should mention that I need to print out 50 random digits between 0 ~ 2**49 - not only primes..it should be totally any random numbers.
                        I downloaded this program and trying to adjust to suite with my aims - that involves with Big Integers.....ki ndly please advise me again...

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          [QUOTE=shana07]
                          Originally posted by JosAH
                          Thank Jos.
                          Ok. I should mention that I need to print out 50 random digits between 0 ~ 2**49 - not only primes..it should be totally any random numbers.
                          I downloaded this program and trying to adjust to suite with my aims - that involves with Big Integers.....ki ndly please advise me again...
                          It doesn't involve BigIntegers at all. Ordinary longs can handle numbers up to
                          2**64. Read the API docs for the Random class and pay special attention to
                          the nextLong() method.

                          kind regards,

                          Jos

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            ps. I changed the thread title.

                            kind regards,

                            Jos

                            Comment

                            • shana07
                              Contributor
                              • Jan 2007
                              • 280

                              #15
                              [QUOTE=JosAH]
                              Originally posted by shana07

                              It doesn't involve BigIntegers at all. Ordinary longs can handle numbers up to
                              2**64. Read the API docs for the Random class and pay special attention to
                              the nextLong() method.

                              kind regards,

                              Jos
                              How about 2**160? It does involve with BigIntegers right. So I need to use nextLong() method....thank u again

                              Comment

                              Working...