Jump to content
Sign in to follow this  
Doolittle

Random number range?

Recommended Posts

Anyone have an algorithm that will take low, mid, and high numbers and give me back a random number? Just like the Triggers use where they have a low number and a high one and a "median". Like low of 1, high of 10 and mid of 8 would mean you're most likely to get on or near 8. Anyone know what I'm talking about? I need to simulate this in a script, not from a trigger. I.e. I wish to expand upon the random command.

Thanks!

Doolittle

Share this post


Link to post
Share on other sites

This is what I got so far:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

randrange = {

_min = _this select 0;

_max = _this select 1;

_calcmin = _this select 2;

_calcmax = _this select 3;

if (_calcmin < _min) then {_calcmin = _min};

if (_calcmax > _max) then {_calcmax = _max};

_calcmin + random (_calcmax - _calcmin);

};

_sum = 0;

for [{_i = 1}, {_i <= (_mid - _min)}, {_i = _i + 1}] do {

_sum = _sum + [_min, _max, _mid - _i, _mid + _i] call randrange;

};

_sum / (_mid - _min); // return value

So...

_min = 1

_mid = 7

_max = 8

Get rand between 6 and 8

Get rand between 5 and 8

Get rand between 4 and 8

Get rand between 3 and 8

Get rand between 2 and 8

Get rand between 1 and 8

Add up & take average

Does this look right?

Share this post


Link to post
Share on other sites

You could try this (untested):

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">//To calculate a random number (_random_no) between two boundaries

_min = _this select 0;

_max = _this select 1;

_range = _max - _min;

_value = ciel(random _range);

_random_no = _min + _value;

if (true) exitWith {};

EDIT: Oops I missed the mid part. Will think about this more.

Share this post


Link to post
Share on other sites

Thanks! But I need it to have a mid point... if that makes since?

Like range of 15 to 40 with mid of 35.

This helped a little: http://www.adit.co.uk/html/random_numbers.html

Talks about a weighted value and stuff. Sigh. I used to know this stuff. Now it looks like alien writing.

Doolittle

EDIT:

http://en.wikipedia.org/wiki/Probability_distribution

http://www.gamedev.net/community/forums/topic.asp?topic_id=267560

Share this post


Link to post
Share on other sites
Thanks! But I need it to have a mid point... if that makes since?

Like range of 15 to 40 with mid of 35.

This helped a little: http://www.adit.co.uk/html/random_numbers.html

Talks about a weighted value and stuff. Sigh. I used to know this stuff. Now it looks like alien writing.

Doolittle

OK take 2 smile_o.gif

Once again untested.

*Other than the _low, _mid and _high values you'll need to specify the probability that the value is going to be near the mid point (needs to be a number between 0 and 1).

*You'll also need to specify the range that the number falls within if its near the mid point. (eg. 2 below or 2 above mid would give a _range_mid = 4)

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_min = _this select 0;

_mid = _this select 1;

_high = _this select 2;

_range_mid = _this select 3;

_probability_of_mid = _this select 4;

_range_all = _high - _min;

_chosen_range = random 1;

if (_chosen_range <= _probability_of_mid) then

{

_value = ciel(random _range_mid);

if (_value <= _range_mid/2) then {_random_no = _mid - _value} else {_random_no = _mid + _value};

} else

{

_value = ciel(random _range_all);

_random_no = _min + _value;

};

if (true) exitWith {};

The only prob with this is there's still a possibility that the value will be near the mid point even if _chosen_range > _probability_of_mid but its going to be slim.

Share this post


Link to post
Share on other sites

Sorry to double post but I've looked through your stats and the median is the same as the 2nd quartile and therefore cuts the data in half ( http://en.wikipedia.org/wiki/Quartile ). Therefore 50% of the data will be below the median value and 50% above this value.

If this is true then something like this should work: <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_min = _this select 0;

_median = _this select 1;

_high = _this select 2;

_range_low = _median - _min;

_range_high = _high - _median;

_probability = random 1;

if (_probabilty <= 0.5) then

{

_value = random _range_low;

_random_no = _min + _value;

}else

{

_value = random _range_high;

_random_no = _mid + _value;

};

if (true) exitWith {};

Share this post


Link to post
Share on other sites

Never heard the word quartile before.. hah.

I think this is what I will do... randrange returns a random between two numbers.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">[[_min, _mid] call randrange, [_mid, _max] call randrange] call randrange

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
Sign in to follow this  

×