Jump to content
Sign in to follow this  
scajolly

How to have "Random" return values in certain Range

Recommended Posts

Hello

Can anyone tell me how to make the random command return values in a predetermined range?

For instance,

"_x setDamage (random 0.2 to 1)" forEach ...

Thank you in advance.

Share this post


Link to post
Share on other sites
randomValue = 
{
 private ["_min", "_max", "_temp"];
 _min = _this select 0;
 _max = _this select 0;
 // normalize
 _temp = random 1; 
 // map to the range
 _temp * (_max - _min) + _min
};

{_x setDammage ([0.2, 1] call randomValue)} forEach ...

Share this post


Link to post
Share on other sites
{_x setDamage ((floor(random 5) + 1) / 5)} forEach units group player;

Edited by kylania
Bah, better solution ninja'd, but mine's one line! :)

Share this post


Link to post
Share on other sites

I assumed he wanted to hit someone between 20% - 100% of their health which I why I used the / 5 in there, so it'll register 0.2, 0.4, 0.6, 0.8 or 1 as the possible results.

Your example was far more flexible unless he's looking for one these five results and a 0.37 wouldn't work for him for example.

Share this post


Link to post
Share on other sites

Thank you both for your replies. These are very good answers.

Kylania, am I correct in guessing that

{_x setDamage ((floor(random 100) + 1) / 100)} forEach units group player;

will do the trick with decent accuracy?

Not to appear thick about it, but how would I go with applying the range 0 to 0.8? (@Kylania)

Share this post


Link to post
Share on other sites

Well since the range is known why not:

{_x setDamage ((random 0.8)+0.2)} forEach units group player;

If you want 0 to 0.8 just use:

{_x setDamage (random 0.8)} forEach units group player;

Share this post


Link to post
Share on other sites
Well since the range is known why not:

{_x setDamage ((random 0.8)+0.2)} forEach units group player;

That could return a number above the range limit of (.8). If the random number gen. returns anything above .6 then the end result will be out of range.

Share this post


Link to post
Share on other sites
That could return a number above the range limit of (.8). If the random number gen. returns anything above .6 then the end result will be out of range.

In his first post he says he wants 0.2 to 1. That's a difference of 0.8 starting at 0.2. I find a number up from 0-0.8 and add 0.2 to that resulting in a number from 0.2-1.0 like his first post.

The second example I gave was a response to:

Not to appear thick about it, but how would I go with applying the range 0 to 0.8?

Share this post


Link to post
Share on other sites

Muzzleflash is perfectly correct :) it was the answer I was going to post.

range 0.2 - 1

((random 0.8) + 0.2)

range 0 - 0.8

(random 0.8)

range -5 - +5

((random 1) - 0.5)

range -10 - +50

((random 60) - 10)

etc.

Edited by DMarkwick

Share this post


Link to post
Share on other sites

Thank you all, this is perfect for my use. =)

Share this post


Link to post
Share on other sites

range -5 - +5

((random 1) - 0.5)

typo - should be:

range -0.5 - +0.5

((random 1) - 0.5)

or:

range -5 - +5

((random 10) - 5)

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  

×