Jump to content
cb65

Random select a number between a range of numbers.

Recommended Posts

Hi all.

Its been awhile since I've done any scripting, just getting back into it now.

 

I need to know how to use random to get a random number between a min of 60 and a max of 240.

 

Thanks.

Share this post


Link to post
Share on other sites

Harzach is very much spot on. I just wanna show some alternatives and a more generic way of getting a random number (just so that you can pick different ranges too).

 

So, if you would like to get a random number in a specific range [a, b] (both a and b inclusive) you could do something like

// I assume this is a function so I get the passed parameters
params[["_min", 0, [0]],	// Minimum of the range
       ["_max", 100, [0]]]; // Maximum of the range

// Calculate random number
private _rand = min + random[max + 1];

// Return the number
_rand;

Alternatively, you could also specify a midpoint in order to get a realisation of a Gaussian distributed random parameter (i.e. a random number extracted from a Gaussian distribution 😐) , centered (its mean) at the midpoint. An example of that would look like (using the same "function" as above)

// I assume this is a function so I get the passed parameters
params[["_min", 0, [0]],		// Minimum of the range
       ["_max", 100, [0]],		// Maximum of the range
       ["_mid", 75, [0]]];		// Midpoint of the range

// Calculate random number
private _rand = min + random[min, mid, max + 1];

// Return the number
_rand;

For more information please have a look at the comments of the respective BIKI page. The first one by Hcpookie regarding getting whole numbers and how each way to do so will affect the resulting distribution and the second about rough distribution numbers on a range [0, 9] from a Gaussian distribution (second code snippet above).

 

One more comment to make here is that, since we are not provided any information on how these numbers are generated we can't be sure about the exact underlying distribution. For example, in many programming languages the "rand()" function provides quasi-random numbers but the higher numbers have higher probabilities (due to implementation specifics of course). The same may, or may not apply here too, but in my opinion (I haven't used the random command extensively though), the provided implementation is more than adequate for game scripting purposes. So, unless you seek to get a "mathematically correct" distribution I believe that random will be more than enough for any other purpose.

 

Hope this helps somehow 😐.

  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×