Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: tor.brandt on Mon 19/12/2016 09:29:08

Title: Random int between two values
Post by: tor.brandt on Mon 19/12/2016 09:29:08
So, the Random command returns a random number between 0 and MAX.
But how would I make it return a random number between some specified minimum value higher than 0 and MAX?
Title: Re: Random int between two values
Post by: Crimson Wizard on Mon 19/12/2016 10:06:32
Quote from: tor.brandt on Mon 19/12/2016 09:29:08
So, the Random command returns a random number between 0 and MAX.
But how would I make it return a random number between some specified minimum value higher than 0 and MAX?

Make it return value between 0 and (MAX - MIN), then add MIN to random result.
E.g. you need value between 10 and 20, do "Random(10) + 10".

Or write convenience function
Code (ags) Select

int RandomMinMax(int min, int max)
{
    return Random(max - min) + min;
}
Title: Re: Random int between two values
Post by: tor.brandt on Mon 19/12/2016 10:17:02
Quote from: Crimson Wizard on Mon 19/12/2016 10:06:32
Make it return value between 0 and (MAX - MIN), then add MIN to random result.
E.g. you need value between 10 and 20, do "Random(10) + 10".

Aahhh yes, of course. So simple, and yet so very very hard for my brain to grasp (laugh)
Thanks!