One option would be to set variable = (|variable| % (MAX - MIN + 1)) + MIN
. This will give you a number that is between MIN
and MAX
, inclusively.
Note that this only works consistently if variable
, MAX
and MIN
are non-negative integers, and MAX >= MIN >= 0
.
The reasoning behind this is as follows: |variable| % (MAX - MIN + 1)
is necessarily non-negative, given that MAX - MIN + 1
and |variable|
are non-negative, and so the modulos must be non-negative. Thus the lowest it can be is zero. We add MIN
after, so the number at its lowest in total can be MIN
.
At most, |variable| % (MAX - MIN + 1)
is MAX - MIN
, due again to the nature of the modulos operator. Adding MIN
afterwards gives us MAX
. Thus the highest output will be MAX
.
No variable
will give a number that is lower than MIN
or higher than MAX
, and there is always an equal number of variable
such that n = (|variable| % (MAX - MIN + 1)) + MIN
, for all n
between MIN
and MAX
inclusively.