473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Large pseudo random numbers

281 100+
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.4028236692093846346337460743177e+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.
Apr 20 '07 #1
24 3041
JosAH
11,448 Expert 8TB
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.4028236692093846346337460743177e+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
Apr 20 '07 #2
shana07
281 100+
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 = 7729326601200590308083947455996107344624824341311
Prime 2 = 7729326601200590308083947455996107344624824341503
Prime 3 = 7729326601200590308083947455996107344624824341643
Prime 4 = 7729326601200590308083947455996107344624824341653
Prime 5 = 7729326601200590308083947455996107344624824341721
Prime 6 = 7729326601200590308083947455996107344624824341823
Prime 7 = 7729326601200590308083947455996107344624824341989
Prime 8 = 7729326601200590308083947455996107344624824342009
Prime 9 = 7729326601200590308083947455996107344624824342049

Expand|Select|Wrap|Line Numbers
  1. import java.math.BigInteger;
  2.  
  3. public class Primes
  4.  {
  5.   // Note that BigInteger.ZERO was new in JDK 1.2, and 1.1
  6.   // code is being used to support the most servlet engines.
  7.   private static final BigInteger ZERO = new BigInteger("0");
  8.   private static final BigInteger ONE = new BigInteger("1");
  9.   private static final BigInteger TWO = new BigInteger("2");
  10.  
  11.    private static final int ERR_VAL = 100;
  12.  
  13.   public static BigInteger nextPrime(BigInteger start) 
  14.   {
  15.     if (isEven(start))
  16.       start = start.add(ONE);
  17.     else
  18.       start = start.add(TWO);
  19.     if (start.isProbablePrime(ERR_VAL))
  20.       return(start);
  21.     else
  22.       return(nextPrime(start));
  23.   }
  24.  
  25.   private static boolean isEven(BigInteger n) {
  26.     return(n.mod(TWO).equals(ZERO));
  27.   }
  28.  
  29.   private static StringBuffer[] digits =
  30.     { new StringBuffer("0"), new StringBuffer("1"),
  31.       new StringBuffer("2"), new StringBuffer("3"),
  32.       new StringBuffer("4"), new StringBuffer("5"),
  33.       new StringBuffer("6"), new StringBuffer("7"),
  34.       new StringBuffer("8"), new StringBuffer("9") };
  35.  
  36.   private static StringBuffer randomDigit() {
  37.     int index = (int)Math.floor(Math.random() * 10);
  38.     return(digits[index]);
  39.   }
  40.  
  41.   public static BigInteger random(int numDigits) 
  42.   {
  43.     StringBuffer s = new StringBuffer("");
  44.     for(int i=0; i<numDigits; i++) {
  45.       s.append(randomDigit());
  46.     }
  47.     return(new BigInteger(s.toString()));
  48.   }
  49.  
  50.   /** Simple command-line program to test. Enter number
  51.    *  of digits, and it picks a random number of that
  52.    *  length and then prints the first 50 prime numbers
  53.    *  above that.
  54.    */
  55.  
  56.   public static void main(String[] args) 
  57.   {
  58.     int numDigits;
  59.     if (args.length > 0)
  60.       numDigits = Integer.parseInt(args[0]);
  61.     else
  62.       numDigits = 150;
  63.     BigInteger start = random(numDigits);
  64.     for(int i=0; i<10; i++) 
  65.     {
  66.       start = nextPrime(start);
  67.       System.out.println("Prime " + i + " = " + start);
  68.     }
  69.   }
  70. }
Apr 20 '07 #3
prometheuzz
197 Expert 100+
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.


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:
Expand|Select|Wrap|Line Numbers
  1. BigInteger start = random(numDigits);
  2. final BigInteger _2pow49 = new BigInteger("562949953421312"); // == 2**49
  3. 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:
http://en.wikipedia.org/wiki/Modulo_operation

Good luck.
Apr 20 '07 #4
shana07
281 100+
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:
Expand|Select|Wrap|Line Numbers
  1. BigInteger start = random(numDigits);
  2. final BigInteger _2pow49 = new BigInteger("562949953421312"); // == 2**49
  3. 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:
http://en.wikipedia.org/wiki/Modulo_operation

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 ....
Apr 20 '07 #5
JosAH
11,448 Expert 8TB
[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:
Expand|Select|Wrap|Line Numbers
  1. /** Simple command-line program to test. Enter number
  2.    *  of digits, and it picks a random number of that
  3.    *  length and then prints the first 50 prime numbers
  4.    *  above that.
  5.    */
The program/class does exactly what your comments state (except for the 50
numbers: you print just 10 of them).

kind regards,

Jos
Apr 20 '07 #6
shana07
281 100+
[quote=JosAH]
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:
Expand|Select|Wrap|Line Numbers
  1. /** Simple command-line program to test. Enter number
  2.    *  of digits, and it picks a random number of that
  3.    *  length and then prints the first 50 prime numbers
  4.    *  above that.
  5.    */
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...thank you
Apr 20 '07 #7
JosAH
11,448 Expert 8TB
[quote=shana07]
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...thank 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
Apr 20 '07 #8
shana07
281 100+
[quote=JosAH]

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:(because my aim is to get 50 random numbers between 0 ~ 2**49).....
Expand|Select|Wrap|Line Numbers
  1.  BigInteger start = random(numDigits);         
  2.     final BigInteger _2pow49 = new BigInteger("562949953421312"); // == 2**49
  3.     start = start.mod(_2pow49); // start is now between 0 (inclusive) and 2**49 (exclusive)*/
What else missing in the codes ......
Apr 20 '07 #9
shana07
281 100+
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
Apr 20 '07 #10
JosAH
11,448 Expert 8TB
[quote=shana07]
Yes, I have tried replace the main method as what has been suggested:(because my aim is to get 50 random numbers between 0 ~ 2**49).....
Expand|Select|Wrap|Line Numbers
  1.  BigInteger start = random(numDigits);         
  2.     final BigInteger _2pow49 = new BigInteger("562949953421312"); // == 2**49
  3.     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
Apr 20 '07 #11
shana07
281 100+
[quote=JosAH]
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.....kindly please advise me again...
Apr 20 '07 #12
JosAH
11,448 Expert 8TB
[quote=shana07]
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.....kindly 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
Apr 20 '07 #13
JosAH
11,448 Expert 8TB
ps. I changed the thread title.

kind regards,

Jos
Apr 20 '07 #14
shana07
281 100+
[quote=JosAH]

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
Apr 20 '07 #15
JosAH
11,448 Expert 8TB
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(String 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.
Apr 20 '07 #16
shana07
281 100+
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(String 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).......Thank you

6004586790292192885711819670706266769686519407577
9267084447242605280803172422681299227158064157338
......//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...
Expand|Select|Wrap|Line Numbers
  1. public class MyBigRandom
  2. {    
  3.   public static void main(String[] args) 
  4.   {
  5.     String out="";    
  6.  
  7.     BigInteger b = null;        
  8.     BigInteger a = new BigInteger("49", 10);  
  9.  
  10.     for (int i = 0; i < 50; i++) 
  11.     {              
  12.         out = getRandom(a.intValue());        
  13.         System.out.println(out);
  14.     }     
  15.   }
  16.  
  17.   public static final String getRandom(int n) 
  18.   {
  19.         if (n < 1) return "";
  20.         StringBuffer sb = new StringBuffer(n);
  21.         java.util.Random r = new java.util.Random();
  22.  
  23.       //  while(r.nextBoolean())
  24.         for (int i = 0; i < n; i++) 
  25.         {
  26.             sb.append(r.nextInt(10));
  27.         }
  28.            return sb.toString();        
  29.   }
  30. }
Apr 23 '07 #17
JosAH
11,448 Expert 8TB
The Random.nextBoolean() 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
Apr 23 '07 #18
shana07
281 100+
The Random.nextBoolean() 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 ...
Expand|Select|Wrap|Line Numbers
  1. public static final String getRandom(int n) 
  2.     {
  3.         if (n < 1) return "";
  4.         StringBuffer sb = new StringBuffer(n);
  5.         java.util.Random r = new java.util.Random();
  6.  
  7.         while(r.nextBoolean())       
  8.         {
  9.             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?
  10.         }        
  11.         return sb.toString(); 
  12.         //return(new BigInteger(sb.toString(), 2));
  13.     }
Apr 23 '07 #19
JosAH
11,448 Expert 8TB
This is a complete give away, I know, but this thread is going nowhere. Run
the following code snippet and see for yourself:
Expand|Select|Wrap|Line Numbers
  1. Random r= new Random(System.currentTimeMillis());
  2. int nofbits= 40;
  3. ...
  4. StringBuffer sb= new StringBuffer();
  5.  
  6. for (int i= 0; i < nofbits; i++)
  7.     sb.append(r.nextBoolean()?'1':'0');
  8.  
  9. 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
Apr 23 '07 #20
shana07
281 100+
This is a complete give away, I know, but this thread is going nowhere. Run
the following code snippet and see for yourself:
Expand|Select|Wrap|Line Numbers
  1. Random r= new Random(System.currentTimeMillis());
  2. int nofbits= 40;
  3. ...
  4. StringBuffer sb= new StringBuffer();
  5.  
  6. for (int i= 0; i < nofbits; i++)
  7.     sb.append(r.nextBoolean()?'1':'0');
  8.  
  9. 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....
Apr 24 '07 #21
JosAH
11,448 Expert 8TB
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
Apr 24 '07 #22
shana07
281 100+
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
Apr 24 '07 #23
JosAH
11,448 Expert 8TB
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.nextBoolean()
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
Apr 24 '07 #24
shana07
281 100+
If you generate n bits at random (uniformly distributed as the Random.nextBoolean()
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...
Apr 24 '07 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
7
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
1
by: LBX | last post by:
I am looking for a large random number generator or ways to take smaller random numbers into large ones. The target size of these random numbers is 512 bits. Any links or codes are nice as I...
57
by: Chris Foote | last post by:
Hi all. I have the need to store a large (10M) number of keys in a hash table, based on a tuple of (long_integer, integer). The standard python dictionary works well for small numbers of keys,...
9
by: Cogito | last post by:
I would like to calculate factorial numbers that produce results of say 100 digits. What is the best way of doing it in Javascript? Can I define a variable and somehow have control on each of its...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
0
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with...
2
by: hmlewis | last post by:
I have a program that allows a user to guess a number 5 times. How can I create the number using a pseudo random number rather than "pre-defining" the numbers. The number has to be between 1 and 20??...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.