Large pseudo random numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by shana07
    How about 2**160? It does involve with BigIntegers right. So I need to use nextLong() method....thank u again
    Hold your horses for a second: I wrote that for numbers up to 2**64 you can use
    longs; for larger numbers the nextLong() method is obviously of no use.

    Have a look at the constructor BigInteger(Stri ng val, int radix) and think
    of the nextBoolean() method in the Random class: i.e. generate each bit one
    by one, stick them in a String and let the BigInteger constructor do the dirty job.

    kind regards,

    Jos

    ps. this is as far as I go because the entire assignment is no fun anymore after
    this last reply.

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #17
      Originally posted by JosAH
      Hold your horses for a second: I wrote that for numbers up to 2**64 you can use
      longs; for larger numbers the nextLong() method is obviously of no use.

      Have a look at the constructor BigInteger(Stri ng val, int radix) and think
      of the nextBoolean() method in the Random class: i.e. generate each bit one
      by one, stick them in a String and let the BigInteger constructor do the dirty job.

      kind regards,

      Jos

      ps. this is as far as I go because the entire assignment is no fun anymore after
      this last reply.
      I do really appreciate your effort to advise me....thank you very much.
      I just don't know how to use nextBoolean() in here...Could you please check my code. Kindly please advise me again (please don't feel bad about my bad programming skill).......Th ank you

      600458679029219 288571181967070 626676968651940 7577
      926708444724260 528080317242268 129922715806415 7338
      ......//Keep writing 50 random numbers
      ...//I do need it to start from 0 (inclusive) to max 49 decimal digits (2**160).


      Here is my program...
      Code:
      public class MyBigRandom
      {    
        public static void main(String[] args) 
        {
          String out="";    
        
          BigInteger b = null;        
          BigInteger a = new BigInteger("49", 10);  
          
          for (int i = 0; i < 50; i++) 
          {              
              out = getRandom(a.intValue());        
              System.out.println(out);
          }     
        }
      
        public static final String getRandom(int n) 
        {
              if (n < 1) return "";
              StringBuffer sb = new StringBuffer(n);
              java.util.Random r = new java.util.Random();
              
            //  while(r.nextBoolean())
              for (int i = 0; i < n; i++) 
              {
                  sb.append(r.nextInt(10));
              }
                 return sb.toString();        
        }
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #18
        The Random.nextBool ean() method returns random true/false values. For every
        true value your can append a '1' to your StringBuffer, for every false value you
        add a '0' to your StringBuffer. Finally use the constructor BigInteger(buf. toString(), 2)
        and voila.

        kind regards,

        Jos

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #19
          Originally posted by JosAH
          The Random.nextBool ean() method returns random true/false values. For every
          true value your can append a '1' to your StringBuffer, for every false value you
          add a '0' to your StringBuffer. Finally use the constructor BigInteger(buf. toString(), 2)
          and voila.

          kind regards,

          Jos
          I have one question about this code, kindly please take a look ...
          Code:
          public static final String getRandom(int n) 
              {
                  if (n < 1) return "";
                  StringBuffer sb = new StringBuffer(n);
                  java.util.Random r = new java.util.Random();
          
                  while(r.nextBoolean())       
                  {
                      sb.append(r.nextInt(10));  // What is this for? as I think it will seed number from 0 - 9. Then where is the relation for max value 2^160?
                  }        
                  return sb.toString(); 
                  //return(new BigInteger(sb.toString(), 2));
              }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #20
            This is a complete give away, I know, but this thread is going nowhere. Run
            the following code snippet and see for yourself:
            Code:
            Random r= new Random(System.currentTimeMillis());
            int nofbits= 40;
            ...
            StringBuffer sb= new StringBuffer();
            		
            for (int i= 0; i < nofbits; i++)
            	sb.append(r.nextBoolean()?'1':'0');
            		
            System.out.println(sb);
            I know you have to put it in a main() method and all, but just try this; I hope you
            can figure out the rest for yourself after all the tips, hints and code snippets that
            have been given to you.

            kind regards,

            Jos

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #21
              Originally posted by JosAH
              This is a complete give away, I know, but this thread is going nowhere. Run
              the following code snippet and see for yourself:
              Code:
              Random r= new Random(System.currentTimeMillis());
              int nofbits= 40;
              ...
              StringBuffer sb= new StringBuffer();
              		
              for (int i= 0; i < nofbits; i++)
              	sb.append(r.nextBoolean()?'1':'0');
              		
              System.out.println(sb);
              I know you have to put it in a main() method and all, but just try this; I hope you
              can figure out the rest for yourself after all the tips, hints and code snippets that
              have been given to you.

              kind regards,

              Jos
              Thank you Jos.. I managed to get it by your help of course. God bless you.
              One question from me...as I need the max length is 160 bits, so I changed nofbits = 160; Why all numbers exhibit about equal lengths? that's 49 or 48 decimal digits?

              Thank you from me....

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #22
                Originally posted by shana07
                Thank you Jos.. I managed to get it by your help of course. God bless you.
                One question from me...as I need the max length is 160 bits, so I changed nofbits = 160; Why all numbers exhibit about equal lengths? that's 49 or 48 decimal digits?

                Thank you from me....
                There's a very small chance that the generated number will be:
                000 ... <160 zero bits here> ... 000 and the decimal representation would be
                a single zero. Think of it this way: how large would the percentage be for all
                numbers between 0 and 100 to be less than 10 (one digit).

                For all numbers between 0 and 10**49 only 1 percent will be less than 10**47

                kind regards,

                Jos

                Comment

                • shana07
                  Contributor
                  • Jan 2007
                  • 280

                  #23
                  Originally posted by JosAH
                  There's a very small chance that the generated number will be:
                  000 ... <160 zero bits here> ... 000 and the decimal representation would be
                  a single zero. Think of it this way: how large would the percentage be for all
                  numbers between 0 and 100 to be less than 10 (one digit).

                  For all numbers between 0 and 10**49 only 1 percent will be less than 10**47

                  kind regards,

                  Jos
                  Herm I got your point Jos.
                  One last favor, after all, do I need to try different approach to get uniformly distributed of lenghts I mean between those range? It just that in my mind to generate test cases that involve with very large number it must be varies of numbers and lenght, what do you think?
                  Again, kindly advise me...thank you

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Originally posted by shana07
                    Herm I got your point Jos.
                    One last favor, after all, do I need to try different approach to get uniformly distributed of lenghts I mean between those range? It just that in my mind to generate test cases that involve with very large number it must be varies of numbers and lenght, what do you think?
                    Again, kindly advise me...thank you
                    If you generate n bits at random (uniformly distributed as the Random.nextBool ean()
                    method does) you get uniformly distributed numbers in the range [0, 2**n) so
                    there's nothing to worry about. e.g. if you generate uniformly distributed numbers
                    in the range [0, 1000) just one percent of the number will be less then 10.

                    kind regards,

                    Jos

                    Comment

                    • shana07
                      Contributor
                      • Jan 2007
                      • 280

                      #25
                      Originally posted by JosAH
                      If you generate n bits at random (uniformly distributed as the Random.nextBool ean()
                      method does) you get uniformly distributed numbers in the range [0, 2**n) so
                      there's nothing to worry about. e.g. if you generate uniformly distributed numbers
                      in the range [0, 1000) just one percent of the number will be less then 10.

                      kind regards,

                      Jos
                      Ok Jos. Thanks for your valueable advice and of cause your effort to reply my questions. Feeling better now..
                      please let me be advised again in future...

                      Comment

                      Working...