-3

I just have a simple question. In Java, we know that the remainder of (y%x) can't be greater than x itself. Thus, we could hypothetically set all numbers less than a certain value of x, say 100. Yet what would be the opposite of this? What if we wanted to set all numbers above a certain value, say 20, to have a range [20,100]?

I was thinking that we could subtract 20 from both sides to have the range [0,80], and then take the modulus of 80, and then add 20 to it.

Am I right in believing this?

Thanks.

kris
  • 375
  • 2
  • 12
  • Or take a value in the range [0, 80] and add 20 to the result. Then you have a value in the range [20, 100]. What range do you want to have at the end? – Elliott Frisch Nov 14 '14 at 18:44
  • 5
    This question appears to be off-topic because it is about math and number algorithms that are not specific to programming nor to Java specifically – ControlAltDel Nov 14 '14 at 18:47
  • @ElliottFrisch, I was hoping to have a value in the range [20,100], but I was unsure how to make all values greater than 20. – kris Nov 14 '14 at 18:47
  • @ControlAltDel, this has to do with modulus. That's in java. – kris Nov 14 '14 at 18:48
  • @user4253715: Java also has strings. Doesn't mean we'll help people with learning English either. – Jeroen Vannevel Nov 14 '14 at 18:51
  • @JeroenVannevel Why do you have to be so rude about it? I understand that this question wasn't exactly how it should have been formatted. But I honestly don't understand your abrasive response. – kris Nov 14 '14 at 19:06

2 Answers2

0

How can the remainder be greater that the divisor ? In other words you division is not complete.

If you want to of from 20-80 you can simple do

int i = 20 + randomInteger % 80;

This will be in the range of 20 -100.

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • Okay, but if you take the mod of 80, then all randomIntegers won't be greater than 80. But then when you add 20, they could be, right? – kris Nov 14 '14 at 18:59
  • the number would be between 0-79 when you add 20 the bounds would be 20-99 ... – StackFlowed Nov 14 '14 at 19:04
0

The Random class includes nextInt(int) which (per the Javadoc) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. So, 0-80 is

Random rand = new Random();
int v = rand.nextInt(81);

If you want it in the ragne 20-100 that would be

int v = rand.nextInt(81) + 20;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you so much @ElliotFrish, this is very helpful! I really appreciate your simple answer and willingness to help. – kris Nov 14 '14 at 19:04