Blockland Forums > Modification Help
Randomizing
Derroith:
So basically I'm trying to learn to use randoms, they're pretty weird and confuse me. I'm trying to make a piece of code that basically randomizes into two little "sections"
I believe I use cases for randoming, I'm not sure, if not correct me.
Okay so I want the code to work like this, when the player does the function, it randomizes out of the two. If you get one, nothing happens, if you get two, it then starts another separate randomize which is randomizing 100-1000.
Also, how would I find a random person on the server?
Brian Smithers:
Random Player:
function getRandomPlayer()
{
for(%i=0;%i<ClientGroup.getCount();%i++)
%pl[%i] = ClientGroup.getObject(%i);
return %pl[getRandom(0,%i)];
}
Random number thing
function randomNumberthing(%client)
{
%num = getRandom(1,2);
if(%num == 2)
nextRandomThing(%client);
}
function nextRandomThing(%client)
{
%num = getRandom(100,1000);
}
phflack:
--- Quote from: Brian Smithers on March 04, 2012, 12:28:22 AM ---Random Player:
function getRandomPlayer()
{
for(%i=0;%i<ClientGroup.getCount();%i++)
%pl[%i] = ClientGroup.getObject(%i);
return %pl[getRandom(0,%i)];
}
--- End quote ---
cool, so torquescript doesn't care about variables initialized being in loops, and doesn't clear them after?
that'd be handy, better than how java does it, sorta like you don't need to declare what type a variable is
Port:
--- Quote from: Brian Smithers on March 04, 2012, 12:28:22 AM ---Random Player:
function getRandomPlayer()
{
for(%i=0;%i<ClientGroup.getCount();%i++)
%pl[%i] = ClientGroup.getObject(%i);
return %pl[getRandom(0,%i)];
}
--- End quote ---
That is rather inefficient. More optimally:
--- Code: ---function getRandomPlayer()
{
if ( !( %cnt = clientGroup.getCount() ) )
{
return -1;
}
return clientGroup.getObject( getRandom( 0, %cnt - 1 ) );
}
--- End code ---
Chrono:
--- Quote from: Port on March 04, 2012, 12:44:00 AM ---That is rather inefficient.
--- End quote ---
Not only inefficient, but may even bring undesired results.
%i will be 1 higher than expected after the loop is done.