how to generate a random long with 4 digits in java? [duplicate] - java

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
how to generate a long that is in the range of [0000,9999] (inclusive) by using the random class in java? The long has to be 4 digits.

If you want to generate a number from range [0, 9999], you would use random.nextInt(10000).
Adding leading zeros is just formatting:
String id = String.format("%04d", random.nextInt(10000));

A Java int will never have leading 0(s). You'll need a String for that. You could use String.format(String, Object...) or (PrintStream.printf(String, Object...)) like
Random rand = new Random();
System.out.printf("%04d%n", rand.nextInt(10000));
The format String %04d is for 0 filled 4 digits. And Random.nextInt(int) will be [0,10000) or [0,9999].

Related

How can I pad a hexadecimal digit with 0 in java? [duplicate]

This question already has answers here:
Integer to two digits hex in Java
(7 answers)
Closed 1 year ago.
I'm trying to pad a hexadecimal digit with 0's in the beginning so that the length of it is always 4. For example, the FF is going to be padded as 00FF. I have tried to use String.format("%04d", number) but it didn't work as my hexadecimal digit is actually a string.
I have also tried using StringUtils.leftPad() but for some reason, IntelliJ couldn't detect it.
Please note my count is going to change, but I haven't represented it here as I'm yet to implement it. This is my sample code. Thanks.
public static void main(String[] args) {
int count = 0;
String orderID = Integer.toHexString(count).toUpperCase();
System.out.println(String.format("%04d", Integer.valueOf(orderID)));
}
String.format("%04x", 255)
with output
00ff
(or use X for uppercases hex digits)
As #user16320675 point, more info about Formatter here.

How does Java store Negative numbers in an Integer Variable? [duplicate]

This question already has answers here:
How does a leading zero change a numeric literal in Java?
(3 answers)
Closed 7 years ago.
How does this snippet of code prints "-511" as the output on the console?
class Test
{
public static void main(String[] args) {
int i = -0777;
System.out.printf("%d",i);
}
}
Is it to do with the way Java stores Negative numbers?
Integer numbers prefixed with 0 are octal numbers. To use decimal numbers remove the 0 prefix:
int i = -777;
Numbers starting with 0 are treated as being in octal by Java. -077 is equivalent to -63, which is what I get when I run your program.
When a number in java code starts with a 0, it interprets it as octal format

arc4random equivalent for Java? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 8 years ago.
using iOS I sometimes like to call 2 functions at random a button press for example, I would use; So sometimes, the user would get (1) the other time (2) etc.
if (arc4random() % 2 == 0) {
// Do one thing (1)
} else {
// Do something else(2)
}
}
How would I do this within Eclipse/java? In otherwords, what is the same statement but in a Java language?
Use the Java Random class. This would give you either 1 or 2:
Random rand = new Random();
int n = rand.nextInt(2) + 1;
nextInt(n) gives you a random number from 0 to n-1 (inclusive). So you have to add 1 to the result.

Random number from 1 - 25 in java sequence [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
So I am trying to generate a random number, but I can't use the Java random function because I need the numbers to be in the range of 1-25. What is the easiest aka most efficient way of doing this? If possible, an explanation would be great!
int random = (int)(Math.random() * 25 + 1);
or
int random = new Random.nextInt(24) + 1;
I prefer to use the Java Random Class.
import java.util.Random;
And then generate the random nuber like this - assuming you want an integer:
Random gen = new Random();
int r = gen.nextInt(25) + 1;
Random numbers between 1 and 25 (1-25)
Random.nextInt(24) + 1

Specify max and min for Random.nextInt()? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Java: generating random number in a range
I want to generate a random int in a logical range. So, say for example, I'm writing a program to "roll" a dice with a specified number of sides.
public int rollDice() {
Random generator = new Random();
return generator.nextInt(sides);
}
Now the problem becomes that this will return values between sides and zero, inclusive, which makes no sense because most dice go from 1 to 6, 9, etc. So how can I specify that nextInt should work between 1 and the number of sides?
To generate a random int value (uniform distribution) between from and to (inclusive) use:
from + rndGenerator.nextInt(to - from + 1)
In your case (1..sides):
1 + rndGenerator.nextInt(sides)

Categories

Resources