You can use the Random class:
int randomNumber = new Random().nextInt(9000) + 1000;
Answer from mehrmoudi on Stack OverflowYou can use the Random class:
int randomNumber = new Random().nextInt(9000) + 1000;
Working solution - different number each time
Random r = new Random();
int fourDigit = 1000 + r.nextInt(10000);
System.out.println(fourDigit);
Broken - same number every time
Random r = new Random(123); // <---- uses same seed every time !
int fourDigit = 1000 + r.nextInt(9000);
System.out.println(fourDigit);
Write a program that generates five random numbers. The number
should be between 1000 and 9999. Then write these number into a
file named “codes.txt”
in java (bouns points if you can give it to me in a pdf file for
java ). :D
How to generate a random four digit number?
how can I generate a random number of any particular number of digits (say 5) in java? - Stack Overflow
write a program that generates five random numbers the number should be between 1000 and 9999 th
Videos
I am trying to generate a random four digit number between 0 and 9999 and so far have the code Random generator = new Random(); regNo = generator.nextInt(9999)
this works great but is there a way to ensure that if the number is less than four digits than it is still in four digit format i.e. 18 should be 0018.
Does the random package also guarantee that each time the method is invoked a different number will be generated than the one previous.
Any help would be much appreciated. Cheers
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
Simple.
- generate a random number upto 89999
- Add 10000 to each generated number.
There's a better way to get random numbers, and that's with java.util.Random. Math.random() returns a double (floating-point) value, but based on your request of a 3-digit number, I'm going to assume that what you really want is an integer. So here's what you do.
// initialize a Random object somewhere; you should only need one
Random random = new Random();
// generate a random integer from 0 to 899, then add 100
int x = random.nextInt(900) + 100;
You are not correct. That can produce numbers under 100. The most familiar way is random.nextInt(900)+100. Here random is an instance of Random.
Before Java 7 there was no reason to create more than one instance of Random in your application for this purpose. Now there's no reason to have any, as the best way is
int value = ThreadLocalRandom.current().nextInt(100, 1000);
private long generateRandomNumber(int n) {
long min = (long) Math.pow(10, n - 1);
return ThreadLocalRandom.current().nextLong(min, min * 10);
}
nextLong produces random numbers between lower bound inclusive and upper bound exclusive so calling it with parameters (1_000, 10_000) for example results in numbers 1000 to 9999.
Old Random did not get those nice new features unfortunately. But there is basically no reason to continue to use it anyways.
public static int randomInt(int digits) {
int minimum = (int) Math.pow(10, digits - 1); // minimum value with 2 digits is 10 (10^1)
int maximum = (int) Math.pow(10, digits) - 1; // maximum value with 2 digits is 99 (10^2 - 1)
Random random = new Random();
return minimum + random.nextInt((maximum - minimum) + 1);
}