Reputation: 2649
I saw many examples to generate random numbers in a specific range [min-max], but i need java code that generates random numbers of n digits or more, so in this case min= 10000000
and no max
.
Note - I am using BigInteger
Upvotes: 0
Views: 864
Reputation: 15706
You can use the constructor BigInteger(int numBits, Random rnd)
to generate positive random numbers with N bits.
As you want to have a minimum, you can add that as an offset to the generated numbers:
Random random = ThreadLocalRandom.current();
BigInteger base = BigInteger.valueOf(10000000); // min
int randomBits = 50; // set as many bits as you fancy
BigInteger rnd = base.add(new BigInteger(randomBits, random));
Upvotes: 3
Reputation: 15685
BigInteger
accepts a decimal String in one of its constructors. Generate individual digits and append them to a String. When you have enough digits in your String, create your BigInteger
from the String. You may want to constrain the first digit to be in [1 .. 9] to avoid leading zeros, depending on your exact requirement.
Upvotes: 1