Currently struggling to figure out how to constrain a value within a range. Meaning, given the random number 219, how can I make sure it is modified to stay within the range of (2, 5).
Here is my current implementation
int min = 2;
int max = 5;
int constrainedValue = (randomValue % max) + min
The above example does not work because (219 % 5) + 2 = (4) + 2 = 6 and obviously 6 is not within my required range.
The purpose of this is to convert a random number into a value that fits within my range. Therefore, I cannot simply mod by just the max or min, the value must be "random" in a sense.
Aucun commentaire:
Enregistrer un commentaire