I want to generate a random number of any particular digit (say 5, i.e., 10000 to 99999) in java. How can I do that?
Asked
Active
Viewed 2,779 times
5 Answers
7
Create a random integer in range 0,10^k - 10^(k-1)
, and add 10^(k-1)
- in your example. range 0,90000
, and add 10000 to it.
Use the Random.nextInt(int) method for it.
Something like:
new Random().nextInt(90000) + 10000

amit
- 175,853
- 27
- 231
- 333
-
This does not support negative ranges. Instead use `rand.nextInt(upper - lower) + lower;` – Upio Sep 04 '14 at 08:51
-
@Upio You are answering a generic question of "how to produce integer in a specific range". This answer aims to show how to create a k digits integer, which is what the OP has asked for. – amit Sep 04 '14 at 08:52
-
You're right. I misinterpreted what he meant by any digit. :) – Upio Sep 04 '14 at 08:53
-
but in case of a 9 or higher digit number what should i do? Integer/int variable will overflow right? please help to make it generic. – sbanerjee Sep 04 '14 at 11:01
-
@sbanerjee You can also use [`nextLong() % number`](http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextLong()) to get a long in range (0,number). If your numbers are bigger than longs, you are going to need a [BigInteger](http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html). You can create several longs until you fill up k-1 digits (including leading zeros), and then geenrate a number in range [1,9], and make it the most significant digit. – amit Sep 04 '14 at 11:49
0
Simple.
- generate a random number upto 89999
- Add 10000 to each generated number.

TheLostMind
- 35,966
- 12
- 68
- 104
0
public int generateRandom() {
Random r = new Random( System.currentTimeMillis() );
return 10000 + r.nextInt(20000);
}
this is from here
or :
for (int i = 0; i < 10; i++) {
System.out.println(Math.round(Math.random() * 89999) + 10000);
}

Community
- 1
- 1

user3145373 ツ
- 7,858
- 6
- 43
- 62
0
The following should work:
public int getRandomNumber(int digits)
{
assert digits > 0; //added because of comment
BigInteger tenPower = new BigInteger("10");
return new Random().nextInt((tenPower.pow(digits).subtract(tenPower.pow(digits-1))).intValue()) + tenPower.pow(digits-1).intValue();
}
Output:
Digits: 1
- Random nextInt Param: 9
- Adding: 1
Random number: 1
Digits: 2
- Random nextInt Param: 90
- Adding: 10
Random number: 66
Digits: 3
- Random nextInt Param: 900
- Adding: 100
Random number: 377
Digits: 4
- Random nextInt Param: 9000
- Adding: 1000
Random number: 1734
Digits: 5
- Random nextInt Param: 90000
- Adding: 10000
Random number: 96602
So it creates the numbers like this:
1...9
10...99
100...999
1000...9999
10000...99999

EpicPandaForce
- 79,669
- 27
- 256
- 428
-
So it's worth noting that for 1 digit, it doesn't generate a `0`. Other than that, it's cool. You can also change it to `long` or keep it as `BigInteger` if you want. – EpicPandaForce Sep 04 '14 at 08:51
-
-
@user1516873 I'm not sure I can understand what `negative digit count` even means, or even a number with `0 digits`. I could just add an assert to prevent that from happening, if you're so inclined. As I said, if you want, you can keep it as a `BigInteger` though if you want to handle large numbers. – EpicPandaForce Sep 04 '14 at 08:55
0
Here is how you can handle negative and positive ranges. Compute a random number within the size of the range and then add the lower bound. E.g. if the range is 100-150, then compute a number between 0 and 50 and add 100. This works for both positive and negative ranges.
import java.util.Random;
public class ArbitraryRange {
public static void main(String []args){
int start = -10000;
int end = -5000;
Random rand = new Random();
int r = rand.nextInt(end - start) + start;
System.out.println(r);
}
}

Upio
- 1,364
- 1
- 12
- 27