1

In java, it's easy to find random number using Random() in a range.

Below is a method I've tried to do that.

public int rand(int min, int max){
    Random rands = new Random();
    int numRand = rands.nextInt(max-min) + min;
    return numRand;
}

But, it's in integer which has 2,147,483,647 as maximum values.

Then, I need to compute some values that has large number.

Let's say the large number is 5234960860151076037807790832396537967963815240976924943830782457906435372625549206966897650382309539933354611312768681026528740101

It contains 130 characters of number. It can't be stored in integer, even in double. But, I need to find large random number like that.

So, I found the way to random large number with using BigInteger.

At here, the random number just for n-digit.

At here, the random number from 0 to n. It's similar with this.

At here, the random number from 1 to 8180385048.

So far, I found them useful. But I'm still confusing how to know whether the minimum number is 0 or 1 or 123456712312.

I want to generate random number using BigInteger from minimum to maximum values, such as from 123456712312 to 12345678910213212312312312312312.

So, how can I find random number from that range (from 123456712312 to 12345678910213212312312312312312)?

Community
  • 1
  • 1
Speaky
  • 185
  • 1
  • 3
  • 15
  • Use SecureRandom.nextBytes. (You'll probably have to figure out the range thing yourself, though.) – Hot Licks Apr 20 '14 at 14:53
  • 1
    1- find the number of digits using an ordinary random generator 2- inside a for loop create a one digit random integer and add it to a string holding the hole random number (you may need to check some conditions to stay in bound) – Mohsen Kamrani Apr 20 '14 at 14:54
  • Consider: If your random numbers are evenly distributed between 123456712312 and 12345678910213212312312312312312 then only one value in 2**20 will be only 12 digits long. So you'd have to sample about one nonillian times (yes, that's a real word) to get a number that small. – Hot Licks Apr 20 '14 at 14:59

0 Answers0