0

I have tried many different ways to generate random numbers but I can't seem to make a simple nextInt(int min, int max) method that will work on negative and positive ranges.

10 to 20 -10 to 10 -10 to -20

If I get it working for one range, the other doesn't work.

user1859040
  • 83
  • 1
  • 3
  • 9
  • 2
    Can you should us the code you wrote? – st0le Nov 28 '12 at 10:11
  • possible duplicate of [Generating a random number within a range (both positive and negative) in java](http://stackoverflow.com/questions/13600421/generating-a-random-number-within-a-range-both-positive-and-negative-in-java) – Has QUIT--Anony-Mousse Nov 28 '12 at 10:12
  • 1
    Possible duplicate of [How to generate random integers within a specific range in Java?](http://stackoverflow.com/questions/363681/how-to-generate-random-integers-within-a-specific-range-in-java) – Theofanis May 09 '17 at 09:09

1 Answers1

1

What have you tried?

rnd.nextInt(max+1-min)+min;

should work as long as min < max. If you want max exclusive, remove the +1.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194