Random r = new Random();

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

Solved

Posted on 2005-04-10

Hi,

I want to creat an array of size 10 of random bigintegers between 1 to N where N is a biginteger.

thanks,

Anu

I want to creat an array of size 10 of random bigintegers between 1 to N where N is a biginteger.

thanks,

Anu

13 Comments

Random r = new Random();

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

BigInteger[] ii = new BigInteger[10];

for (int i = 0; i < 10;i++) {

ii[i] = new BigInteger(32, rand) ;

}

>> 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.

Mayank's suggestion is good, but that would be

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

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,51

Title | # Comments | Views | Activity |
---|---|---|---|

factorial example challenge | 10 | 44 | |

array11 challenge | 16 | 40 | |

Saving Tweets with emojis using Twitter4J in a Mysql table | 2 | 18 | |

solarwind tftp server | 2 | 18 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**19** Experts available now in Live!