1

Possible Duplicate:
Generating random number in a range with Java

I am trying to create a random number that is above the value of one boundary, and below another. The number can be equal to either of the boundaries too.

Both boundaries are created from random. highNumber is a random between 0 and 100, and lowNumber is a random between 0 and half of highNumber.

At the moment my code is as follows:

    public static void createCorrectNumber() {
            random = new Random();
            correctNumber = random.nextInt(highNumber)+1;
            correctNumber -= lowNumber;
    }

This is not functional, as when the lower bound is taken away from it, it can become lower than the boundary. Any ideas?

Community
  • 1
  • 1
JamoBox
  • 749
  • 7
  • 22
  • I should also mention both boundaries are created from random. highNumber is a random between 0 and 100, and lowNumber is a random between 0 and half of highNumber. – JamoBox Dec 29 '12 at 12:09
  • 1
    edit your original post with this added precision, then – fge Dec 29 '12 at 12:11
  • 1
    what about http://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java isnt this the same question?? – wmax Dec 29 '12 at 12:12
  • edited the question with that info – JamoBox Dec 29 '12 at 12:13

1 Answers1

4

use

correctNumber = random.nextInt(highNumber - lowNumber + 1) + lowNumber;
Henry
  • 40,427
  • 6
  • 56
  • 72