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.
24 3041
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
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 -
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);
-
}
-
}
-
}
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: - 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: http://en.wikipedia.org/wiki/Modulo_operation
Good luck.
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: - 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: 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 ....
[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: -
/** 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
[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: -
/** 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...thank you
[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
[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)..... - 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 ......
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
[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)..... - 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
[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...
[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
ps. I changed the thread title.
kind regards,
Jos
[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
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.
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... - 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();
-
}
-
}
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
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 ... - 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));
-
}
This is a complete give away, I know, but this thread is going nowhere. Run
the following code snippet and see for yourself: -
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
This is a complete give away, I know, but this thread is going nowhere. Run
the following code snippet and see for yourself: -
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....
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
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
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
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...
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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??...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |