Jump to content
Sign in to follow this  

Randomizing Group Behaviour

Recommended Posts

So I'm working on a script that randomly creates groups of units of random size and make by a series of loops. To keep things short, here is my current problem area:



Primarily note that these are snippets taken from the code taken sequentially.

	_randomPatrol = '[_group, _position, random 800] call BIS_fnc_taskPatrol';
	_garrison = '[_group, _position, random 300, 3, true] call CBA_fnc_taskDefend';
	_behaviorPool = [_randomPatrol, _garrison];

	// Group random patrol or garrison function
	_randomBehavior = selectRandom _behaviorPool;

What I want to do is make it a random pick of whether units are tasked with defending a location, or patrolling the location. It works when I set it so that all of the units garrison, or all of the units patrol, but not for randomizing it. I'm assuming it's a problem with array or something?


Any help is appreciated, thanks.

Share this post

Link to post
Share on other sites

if (random 1 < 0.49) then {

     null = [_group, _position, random 800] call BIS_fnc_taskPatrol;

} else {

null = [_group, _position, random 300, 3, true] call CBA_fnc_taskDefend;


Share this post

Link to post
Share on other sites

You can actually go nuts with randomizing behavior.

The basic concept as suggested by davidoss could be enhanced like this:

_mode = selectRandom [0,1];

if (_mode isEqualTo 0) then {

    _patrol = [_group, _position, random 800] call BIS_fnc_taskPatrol;


if (_mode isEqualTo 1) then {

    _defend = [_group, _position, random 300, 3, true] call CBA_fnc_taskDefend;


This way you avoid if then else constructs and random scalar, since you already know that you either want A or B to happen.

Adding further behaviors is as easy as adding a number and an if statement, so you can enhance it pretty much on the fly with least time spent.


However you can always step it up a notch and use BIS_fnc_selectRandomWeighted:

_modes = [0,1];
_weights = [0.7,0.3];
_mode = [_modes,_weights] call BIS_fnc_selectRandomWeighted;

which result in the first mode being picked 7 out of 10 times, and the second mode being picked 3 out of 10 times.

This way you get a better distribution of modes instead of using regular random, which results in a normal distribution.

Advantage is that you can set most groups to patrol the area, with a few groups garrisoning a building but it won't ever be the same amount of groups doing the same behavior.

On top of that you could randomize the _weights to add more unexpected behavior, just make sure the value doesn't go above 1.


Talking about distribution you might want to replace random 800 and random 300 with the newly added gaussian distribution:

random [200,600,800];

Read up on the wiki page which explains it really well, leads to more authentic distances in your case, since random 300 could very well result in an oddly low number like 2 or 20.



You could also replace 0,1,2,n with strings for better readability, which comes in handy if you want to randomly pick between 50+ behaviors like this:

_modes = ["PATROL","DEFEND"];
_weights = [random [0.4,0.6,0.7],random [0.2,0.3,0.4]];
_mode = [_modes,_weights] call BIS_fnc_selectRandomWeighted;

if (_mode isEqualTo "PATROL") then {

    _patrol = [_group, _position, random [200,600,800]] call BIS_fnc_taskPatrol;


if (_mode isEqualTo "DEFEND") then {

    _defend = [_group, _position, random [150,250,300], 3, true] call CBA_fnc_taskDefend;


As you see it's incredibly easy to enhance this snippet with further modes.



Share this post

Link to post
Share on other sites

Wow, I feel very stupid now that I realize I could just use a simple if statement... But yeah I'm going to get into it with that last example I think! Thanks guys!

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