• November 4th, 2011, 09:30 PM
tripline
The random number should be between number1 and number2, an inclusive but after the first random number request, it goes outside my stipulation.
Code Java:

```import java.util.*; public class RandomNumber{ public static void main(String[] args) { Random variable = new Random(); Scanner sc = new Scanner(System.in); System.out.println("Enter number 1: "); int number1 = sc.nextInt(); System.out.println("Enter number 2: "); int number2 = sc.nextInt();   if(number1>number2) { int a = (variable.nextInt((number1-number2) + number2)); System.out.println(a); } else { int a = (variable.nextInt((number2-number1) + number1)); System.out.println(a); } } }```
• November 4th, 2011, 10:57 PM
Tjstretch

Secondly, your naming is terrible, the variable name should give you an idea of what the variable is.

Be Explicit
and
Posting Code
• November 4th, 2011, 11:04 PM
skaterboy987
THIS IS NOT THE MOST EFFICIENT WAY TO DO THIS. BUT IT IS THE EASIEST IN MY OPINION:

Okay consider this:
While your random number is smaller than the smallest number OR your random number is bigger than the biggest number you need to keep retrying
Create a random number up to the largest number and let that be random's value.
Once random meets your qualifications the loop will stop.
Then outside your while loop, the variable random is a number that fits your qualifications.

• November 4th, 2011, 11:14 PM
Tjstretch
At skaterboy987, that code is not just inefficient but also not as simple as it could be. (You can get the number in one line of code, no loops)

At tripline, thank you for highlighting your code; now for generating a random number, you only have a certain amount of randomness. EG if you were trying to find a number between 2 and 4 inclusive, you would only have a range of 3 possible different numbers (2, 3 and 4). Now, how could you use that to generate a number between the first number and the second number?
• November 4th, 2011, 11:39 PM
tripline
skaterboy - you are saying do a while loop until the random numbers hit between number1 and number2? I guess I can try that.
Tjstretch - What line of code are you talking about? I did a fair amount of research and tried to do it as simple as possible as well.
(variable.nextInt((2) +2 ) ?
Starting at 2, I can only go up by 2?
• November 5th, 2011, 12:08 AM
Tjstretch
Yes, that is the basics of the idea, but since you are having trouble I will try to help you a little bit more:
Code Java:

```int smallestNumber = 2; //This could be anything int largestNumber = 4; //Same int range = largestNumber - smallestNumber; //What is the range of randomness in the numbers?```
Technically, I suppose this is two lines if you include range as one of lines of code.
Now, with those numbers, how could you devise a Random statement that would go between the smallest number and the largest number? Hint, you will have to use addition outside of the nextInt method.

Note that in order to be inclusive, you will have to add one in your gaining of the random number. (This is intuitively obvious when you see the rest of the line of code and note that nextInt(n) is between 0 (inclusive) and n (exclusive))
• November 5th, 2011, 12:27 AM
tripline
Well I feel like an idiot. My parentheses were in the wrong place. I had to rewrite the entire thing to figure it out. Really wish someone would have said syntax error.

Code Java:

```import java.util.*; public class RandomNumber{ public static void main(String[] args) { Random variable = new Random(); Scanner sc = new Scanner(System.in); System.out.println("Enter number 1: "); int number1 = sc.nextInt(); System.out.println("Enter number 2: "); int number2 = sc.nextInt(); int range1 = (number1 - number2); int range2 = (number2 - number1);   if(number1>number2) { int a = (variable.nextInt(range1)) + number2; System.out.println(a); } else { int a = (variable.nextInt(range2)) + number1; System.out.println(a); } } }```
• November 5th, 2011, 12:49 AM
Tjstretch