# Generating a random number from a range of inputted numbers

• September 25th, 2013, 01:11 PM
R0bert199O
Generating a random number from a range of inputted numbers
Hello,

I am trying to randomly generate a number from a range of numbers.

Code :

``` public static void defineLucky(){ System.out.println("\nChoose a lucky number from a range " + "of numbers \n"); System.out.println("Enter lo number: "); int lo = scanner.nextInt(); System.out.println("Enter hi number: "); int hi = scanner.nextInt(); System.out.println(""); System.out.println("Your lucky number is: " + getLucky(lo,hi)); } public static int getLucky(int lo, int hi){ int total = 0; for (int i = lo; i <= hi; ++i){ total += i; } return total; }```

Example: Right now when I put 5 and 10 in it is giving me 45 and I'm not really sure what needs done to fix this. I'm wanting it to give me a random number between 5 and 10.
• September 25th, 2013, 01:24 PM
KevinWorkman
Re: Generating a random number from a range of inputted numbers
That's exactly what I would expect it to give you. 5+6+7+8+9+10 = 45. Why would this code give you a random number of any kind?
• September 25th, 2013, 01:34 PM
R0bert199O
Re: Generating a random number from a range of inputted numbers
It's not and that's my problem. I've been trying to figure out how to get it to work without any luck. That was just my latest attempt, and only thing that wasn't giving me errors.
• September 25th, 2013, 01:48 PM
KevinWorkman
Re: Generating a random number from a range of inputted numbers
Take a look at the Math.random() function or the Random class.