Solved

# array of random BigInteger

Posted on 2005-04-10
458 Views
Hi,
I want to creat an array of  size 10 of  random bigintegers between 1  to  N where N is a biginteger.
thanks,
Anu

0
Question by:anumit

LVL 3

Expert Comment

long[] myArray = new long[10];
Random r = new Random();
for( int i=0; i<10; i++ ) long[i]=r.nextLong();
0

LVL 3

Expert Comment

With real random bigintegers, that are arbitrary length (!), you would nearly always exhaust your memory :)
0

LVL 3

Expert Comment

ups, you have a given N, sorry :) I would create a string representation of the integer, by adding up to n numbers. And then assigning them.
0

LVL 86

Accepted Solution

java.util.Random rand = new java.util.Random();
BigInteger[] ii = new BigInteger[10];
for (int i = 0; i < 10;i++) {
ii[i] = new BigInteger(32, rand) ;
}
0

LVL 3

Expert Comment

Respect, CEHJ, you are good :)
0

LVL 30

Expert Comment

>> random bigintegers between 1  to  N where N is a biginteger
>> ii[i] = new BigInteger(32, rand) ;

I guess the '32' should ideally be replaced with some variable obtained from 'N':

>> between 1  to  N where N is a biginteger

2 ^ numBits - 1 = N
=> 2 ^ numBits = N + 1
=> numBits = log ( N + 1 ) / log 2

That perhaps makes it:

ii[i] = new BigInteger ( Math.log ( N.doubleValue () ) / Math.log ( 2 ), rand ) ;

- Mayank.
0

LVL 86

Expert Comment

Thanks Mig-O

Mayank's suggestion is good, but that would be

ii[i] = new BigInteger ((int)(Math.log (N.doubleValue()) / Math.log(2)), rand);
0

LVL 30

Expert Comment

Argh, forgot to do the (int) conversion :)
0

LVL 21

Expert Comment

BigInteger already does this calculation for you so all you need to do is:

ii[i] = new BigInteger (N.bitLength(), rand);

NOTE: the N would have to be a multple of 2^N-1 (1,3,7,15,31,63,127,255,511,...)our it would be rounded UP to the nearest multiple.  For example if N was 8 you would get random numbers from 0-15, if N were 256 you would get random numbers from 0-512.
0

LVL 3

Expert Comment

This leads us to this Equation:

ii[i] = new BigInteger(N.bitLength()*10, rand).divideAndRemainder(N)[1];

This gives values exactly from 0 to N-1, but the values in some upper part of the number range apprear less with a propability of 1/2^10=~0,1%, what seems acceptable.
0

LVL 3

Assisted Solution

ii[i] = new BigInteger(N.bitLength()*10, rand).mod(N);

this is shorter and does the same :)
0

LVL 30

Expert Comment

This has been an interesting discussion ;-) revived a lot of Maths I did in college :)
0

LVL 86

Expert Comment

:-)
0

## Featured Post

### Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …