Math variables (random)

In order to get a specific range of values first, you need to multiply by the magnitude of the range of values you want covered.

random * ( Max - Min )

This returns a value in the range [0,Max-Min), where ‘Max-Min’ is not included.

For example, if you want [5,10], you need to cover five integer values so you use

random * 5

This would return a value in the range [0,5], where 5 is not included.

Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.

Min + (random * (Max - Min))

You now will get a value in the range [Min,Max]. Following our example, that means [5,10]:

5 + (random * (10 - 5))

But, this still doesn’t include Max and you are getting a double value. In order to get the Max value included, you need to add 1 to your range parameter (Max - Min) and then round the decimal part to two decimal points. This is accomplished via:

Min + round(random * ((Max - Min) + 1);2)

And there you have it. A random integer value in the range [Min,Max], or per the example [5,10]:

5 + round(random * ((10 - 5) + 1);2)
2 Likes