Generating a 2 digit integer [duplicate] - java

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 6 years ago.
I need to generate a random 2 digit number which will be concatenated to the end of a string. The number MUST be 2 digits (i.e. 00-99, nothing <10 is acceptable).
How can this be done using Java libraries in the simplest way possible?

A pretty simple way would be to generate two random integers separately, and then add them to the end of your string. That way anything from 1-9 would show up as 01-09.
Using this answer, Generating random integers in a specific range, you generate the two integers. Then create an empty string and add the two variables you used to store your random integers.
Psuedocode:
int a = RANDOMNUMBER
int b = RANDOMNUMBER
String number = "" + a + b

Related

new Random().nextInt(999999) sometimes generates 5 digit number [duplicate]

This question already has answers here:
Java Generate Random Number Between Two Given Values [duplicate]
(7 answers)
Closed 5 years ago.
I am using new Random().nextInt(999999) to generate a 6 digit random number. But sometimes it generates 5 digit numbers. And I explicitly have to check if generated number is not 6 digit.
Why this method does that? And is there any other way to be pretty sure it generate only 6 digit random number.
Random().nextInt(999999) generates number between 0 and 999998, so you have ~10% chance to get number smaller than 100000 (5 and less digits).
Try
100000 + Random().nextInt(900000)
you will get number in range 100000 - 999999
Random().nextInt(999999) generates 5 digits number sometimes, because it generates random values between 0 and 999999.
Use the following snippet for generating random numbers in a range:
Random random = new Random();
minimum + random.nextInt(maximum - minimum + 1);

How to work with numbers of the order 10^100 in C & java? [duplicate]

This question already has answers here:
How to work with BIG numbers? [duplicate]
(2 answers)
Large Numbers in Java
(6 answers)
Closed 6 years ago.
How to work with numbers of the order 10^100?Like iterating upto a number of that order,getting sum of squares of their digits;then checking if the sum matches the square of a certain number,if yes sum up those numbers and finally display the sum of those numbers.
You can easily handle this kind of task with strings containing the decimal representation of the numbers.
The sum of squares of the digits of a 100 digit number is bounded by 100*9^2 = 8100: you can use int for that.

How to specify ranges to java.util.Random? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 6 years ago.
I don't know why Java's designers didn't design java.util.Random objects to return random numbers like this:
rand.nextInt(minValue, maxValue);
I don't understand how to pass min and max values to this function. I've seen this recommended:
random.nextInt(max + 1 - (min)) + min
but what does that mean? Does that mean:
random.nextInt(FULL_RANGE + 1) + START_OF_RANGE
For example, if I wanted to generate values between -1 and 1, inclusive, is this how I do it:
int oneOfThreeChoices = rand.nextInt(1 + 1 + 1) - 1;
The API provides sufficient tools for you to complete this task. If you want an random int on the range [a,b], inclusive on both ends, use
int n = random.nextInt(b-a+1) + a
assuming that random is an object previously declared to be of type java.util.Random.
random.nextInt(bound) returns random number from 0 to bound (exclusive). For example random.nextInt(10) returns random number from 0 to 9.
So, if you want to return number from 5 to 15 (inclusive) just say
random.nextInt(11) + 5
Why java designers have not included more convenient API to JDK? I think because the way explained here is pretty simple and obvious.

nextInt in java i don't understand how this works [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
I'm new to java and was viewing a tutorial and saw nextInt, the guy said that it was supposed to give me a number between -5 and 5 but i have two problems :
(r.nextInt(5 - -5) + -5)
1)In what order does it operate
2)The explanation that i found says that it doesn't accept negatives then why is it supposed to allow me to give me -5?
Thanks a lot.
The answer is unary negative and subtration. Usually, that's written as +. Like
(r.nextInt(5 + 5) + -5)
which is
(r.nextInt(10) - 5)
I assume this is for a Random, in which case it returns [0,10). Which if you subtract 5, is [-5,5).
What your code will do is generate a random integer between 0 and the specified value, and will then subtract 5 from the generated integer.

Display numbers with leading zeros (0) in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Add leading zeroes to number in Java?
Say that I have to add two integers one being 0001 and the other 0002. If I add it in java then I get 3 however I would like 0003. Would I have to make a loop to map out the zeros or is there an easier way.
Don't confuse numbers with String representation of numbers. Your question revolves around the latter -- how to represent a number as a String with leading zeros, and there are several possible solutions including using a DecimalFormat object or String.format(...).
i.e.,
int myInt = 5;
String myStringRepOfInt = String.format("%05d", myInt);
System.out.println("Using String.format: " + myStringRepOfInt);
DecimalFormat decimalFormat = new DecimalFormat("00000");
System.out.println("Using DecimalFormat: " + decimalFormat.format(myInt));
you can add a left pad with zeros after having the result.
String.format("%05d", result);
for zero-padding with length=5.
EDIT: i to removed the previous EDIT, it was totally wrong :#
This will help you
String.format (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax)
In your case it will be: String.format("%03d", num) - 0 - to pad with zeros, 3 - to set width to 3

Categories