Jump to content
Sign in to follow this  
longrange

Random Integers

Recommended Posts

Hello, I am really new to scripting in Arma with SQF but I do have experience with other programming languages. My question is how do I go about generating a random integer. I am trying to create a super simple script that is run on initialization of some enemy soldiers, and basically i want it to randomly apply this effect to some of them, and apply a different effect to the rest of them. this is the code that I have right now and it will always go to the else scope, it never says "init mov" so I'm guessing that I am not implementing random correctly. thanks.

num = (random 2);

if(num == 1)then

{

p1 sidechat "init mov";

}

else

{

p1 sidechat "init non";

};

Share this post


Link to post
Share on other sites

You don't necessarily have to use integers. You could modify your condition by checking "if (_num > 0.5) then...". Or use "round(random n)" to get integers.

Share this post


Link to post
Share on other sites

The thing about random is that it does not actually output an int, but rather a double. To get an int, you'll have to use floor(random 2). On that part, you'll have to keep in mind that floor will always scale down, and not logically. So 2.99 will convert to 2 (opposite of that command is ceil), 3.14 to 3 etc. Generally I'd recommend using x.99 instead of your regular x when using floor (in your case x = 2) to help with it always rounding down.

Share this post


Link to post
Share on other sites

the random command never returns the limit. So if you do random 2, it can go from 0 to 1.99. This means that floor has an even distribution by default.

if (floor(random 2) == 0) then {

} else {

};

Edited by SilentSpike

Share this post


Link to post
Share on other sites

I've done something very similar, i.e. randomly choosing between two options, like this:

_num = floor (random 2); //yields either 0 or 1
_str = if (_num == 1) then {"init mov"} else {"init non"};
_p1 sidechat _str;

EDIT: Spike beat me to it...

Share this post


Link to post
Share on other sites

_num=[1,2] call BIS_fnc_randomInt;

Lot of function are developped by BIS, you just have to find them in the Editor/ Fx.

Share this post


Link to post
Share on other sites

Really unnecessary for something simple like this however. It's more efficient to just use 2 commands than call + however many more are in that function.

Share this post


Link to post
Share on other sites
Really unnecessary for something simple like this however. It's more efficient to just use 2 commands than call + however many more are in that function.

Maybe, but his question was "how do I go about generating a random integer" !

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  

×