-2

So stop me if you've heard this one. I've already looked all over this and other forums to see if anyone had tried this, and I can't find it for the life of me.

I'm trying to generate a random number, but the Max/Min are variables. The values are derived from a text file and can be changed at will by the user.

Is there a way to make a single rn.nextInt such that the Max/Min could be both negative, both positive, one positive and one negative, or the same number (including both being 0)?

The purpose is to have a setting in my game where the user can choose a markup value on purchasable items, but I want to allow discounts as well. I also want them to have the option to set both values to 0 and just use the stock value of the items.

I have tried a few workarounds already, including:

//for Max = 0, Min = 0
int n = (rn.nextInt((a) + (b + 1000))) - 1000

which I really hoped would work.

Any help would be greatly appreciated.

MacKenzie
  • 13
  • 2
  • Sorry, the output value for the workaround was just some number between 0 to 1000 minus 1000, so I'd just get a negative number, instead of getting 0 everytime, which is the goal. – MacKenzie Jan 30 '16 at 00:18
  • What output do you expect? If you want `0`, use `0`. If you want a random number use a random number. – Elliott Frisch Jan 30 '16 at 00:19
  • Possible duplicate of [Generating random integers in a specific range](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range) – Tom Jan 30 '16 at 00:51
  • Not a duplicate of that question. This one has an important extra requirement ... the min and max can be negative. That makes a big difference to the answers. – Stephen C Jan 30 '16 at 00:55
  • @ElliottFrisch - It is not quite as simple as you imagine :-) – Stephen C Jan 30 '16 at 00:58

1 Answers1

0

I'm trying to generate a random number, but the Max/Min are variables.

Something like this:

 public int randomInRange(Random rn, int min, int max) {
     return (int)(rn.nextLong(((long) max) - min + 1) + min);
 }

This should work for all min or max, provided that max >= min. (We have to use nextLong because a nextInt would throw an exception if the "range" is bigger than 2^31 - 1.)

Is there a way to make a single rn.nextInt such that the Max/Min could be both negative, both positive, one positive and one negative, or the same number (including both being 0)?

Something like this:

 public int randomBetween(Random rn, int range1, int range2) {
     return randomInRange(rn, 
                          Math.min(range1, range2), 
                          Math.max(range1, range2))
 }

Obviously, you can refactor the above methods to inline the first one into the second one. If written it as above to help you understand how I figured out the solution.

CAVEATS: I haven't tested the above, and it is possible that there are edge cases involving integer overflow and/or Integer.MIN_VALUE, etc. (But I don't think so.)

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • So it turns out I'm just silly and was trying to rn.nextInt((Max + 1 - Min) + Min) so I was just adding the Min right back into the random number. Totally get how Random works now, and yes, it works for all values that aren't too big for the variable type as long as Max > Min (at least all combinations I've tested). Thanks everybody! – MacKenzie Jan 30 '16 at 01:13