Jump to content
Sign in to follow this  
Pasi

Random Array

Recommended Posts

Hello guysy

 

i have a new Problem :/

 

so i have a Array like this:

_jobs = 
        [
	["BLABLATEXT1","BLABLATEXT1","BLABLATEXT1","BLABLATEXT1",'//SCRIPT'],
	["BLABLATEXT2","BLABLATEXT2","BLABLATEXT2","BLABLATEXT2",'//SCRIPT']
];

an now i want to select one of them randomly like this:

randomselect = selectRandom _jobs;

but allways it sayd that randomselect is not defined :/

anyone a idea ?

 

pls help me :D

Pasi

 

Share this post


Link to post
Share on other sites

Works for me
 

_jobs =  
        [ 
 ["BLABLATEXT1","BLABLATEXT1","BLABLATEXT1","BLABLATEXT1",'//SCRIPT'], 
 ["BLABLATEXT2","BLABLATEXT2","BLABLATEXT2","BLABLATEXT2",'//SCRIPT'] 
];

hint str selectRandom _jobs;

hint shows ["BLABLATEXT2","BLABLATEXT2","BLABLATEXT2","BLABLATEXT2","//SCRIPT"]

Share this post


Link to post
Share on other sites
Guest

Your variable may be in the wrong scope.

Share this post


Link to post
Share on other sites

It dont work :/

 

the Variables are defined in a Switch do case but that ist not the Problem or ? :/

_class = 1;
switch (_class) do
{
case 1:
{
_jobs =  
        [ 
 ["BLABLATEXT1","BLABLATEXT1","BLABLATEXT1","BLABLATEXT1",'//SCRIPT'], 
 ["BLABLATEXT2","BLABLATEXT2","BLABLATEXT2","BLABLATEXT2",'//SCRIPT'] 
];
randomselect = selectRandom _jobs;
};
case 2:
{
_jobs =  
        [ 
 ["BLABLATEXT3","BLABLATEXT3","BLABLATEXT3","BLABLATEXT3",'//SCRIPT'], 
 ["BLABLATEXT4","BLABLATEXT4","BLABLATEXT4","BLABLATEXT4",'//SCRIPT'] 
];
randomselect = selectRandom _jobs;
};
};

Share this post


Link to post
Share on other sites

 

It dont work :/

 

the Variables are defined in a Switch do case but that ist not the Problem or ? :/

_class = 1;
switch (_class) do
{
case 1:
{
_jobs =  
        [ 
 ["BLABLATEXT1","BLABLATEXT1","BLABLATEXT1","BLABLATEXT1",'//SCRIPT'], 
 ["BLABLATEXT2","BLABLATEXT2","BLABLATEXT2","BLABLATEXT2",'//SCRIPT'] 
];
randomselect = selectRandom _jobs;
};
case 2:
{
_jobs =  
        [ 
 ["BLABLATEXT3","BLABLATEXT3","BLABLATEXT3","BLABLATEXT3",'//SCRIPT'], 
 ["BLABLATEXT4","BLABLATEXT4","BLABLATEXT4","BLABLATEXT4",'//SCRIPT'] 
];
randomselect = selectRandom _jobs;
};
};

 

Should work, put

hint str [randomselect];

right after randomselect = .... statement, what does it show?

Share this post


Link to post
Share on other sites

Nothing :/

 

i dont see a Hint.

in the Debug console over Watch when i typing randomselect  i see this: []

Share this post


Link to post
Share on other sites

it cannot be [] unless you are setting it to [] elsewhere, perhaps through some loop or _jobs is somethng like [[],[]]? Also, are you running this on the server? Because there is no reason why hint wouldn't show at all

Share this post


Link to post
Share on other sites

Hello there. Sorry to hijack the thread, but I am also trying to get my head around a bit of randomness :icon_biggrin:

 

So, take the following:

_numberArray = [1, 2, 3];
_number = _numberArray call BIS_fnc_selectRandom;

How would one make the randomly generated number of selections from an array? For example, say our random number is 3.

_array = [variable_01, variable_02, variable_03, variable_04, variable_05];

How could we choose the random number (in this case, 3) from the above array?

 

I'm sure the answer is pretty simple - probably involves the 'count' command - but just can't quite nail it.

Share this post


Link to post
Share on other sites
//if you want to use array;
_numberArray = [0, 1, 2];
_number = selectRandom _numberArray;//0,1,or 2

//or if just generate numer
_number = floor random 3;//0,1,or 2

//your choose
//for example your _number has value 2
_array = [variable_01, variable_02, variable_03, variable_04, variable_05];
_myselect = _array select _number; // variable_03



//there are more posibilities
//you can for example just do something like that:

_array = [variable_01, variable_02, variable_03, variable_04, variable_05];
_myselect = _array select (floor random (count _array));

//or

_array = [variable_01, variable_02, variable_03, variable_04, variable_05];
_myselect = selectRandom _array;

 

  • Like 1

Share this post


Link to post
Share on other sites

Thank you for the response!

 

However, unless I'm being really silly and have misunderstood, I would like to pull a random number of variables from the array. Are the examples above only for a single variable?

 

A bit like this, I guess:

 

Random Number between 1-3 >>> that random number of variables chosen randomly from the array >>> returns a random number of random variables (so - maybe the random number would be two and we'd end up with variable_02 and variable_05, or something).

Share this post


Link to post
Share on other sites

You can select one index from array using select.

But you can make  a simply loop

or function to get random array  of indexes from defined  array.

 

 private _fnc_getrandindexes = {
 
	params [ ["_array",[],[[]]], ["_indexcnt",1,[0]] ];
	
	if (_array isEqualTo []) exitWith {
		//exit if passed _array is empty or not passed, if _indexcnt is not passed using 1
		diag_log format ["_fnc_getrandindexes: Passed array is empty: %1 - exiting...",str _array]; 
	};
	
	//define output
	private _return_array = [];
	
	//some other prevents
	if (_indexcnt isEqualTo 0) then {_indexcnt = 1};
	if (_indexcnt > count _array) then {_indexcnt = count _array};
	if (_indexcnt isEqualTo (count _array)) exitWith {
		// exit with shuffled array if indexes count == _indexcnt
		_return_array = _array call BIS_fnc_arrayShuffle;
		_return_array
	};
	//loop
	for "_i" from 1 to _indexcnt do {

		private _randindex = selectRandom _array;
		_return_array pushBack _randindex;
		_array = _array - [_randindex];
	};
	//return
	_return_array
};


_array = [variable_01, variable_02, variable_03, variable_04, variable_05];
private _myrandomindxcnt = floor (random (count _array));

private _mynewarray = [_array, _myrandomindxcnt] call _fnc_getrandindexes;
//or if you want to change existing array
_array = [_array, _myrandomindxcnt] call _fnc_getrandindexes;

 

  • Like 1

Share this post


Link to post
Share on other sites

Thank you, Davidoss - I think it makes sense to me, a bit, now! :)

 

 

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  

×