Author Topic: Randomizing  (Read 1448 times)

 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?

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);
}

Random Player:

function getRandomPlayer()
{
   for(%i=0;%i<ClientGroup.getCount();%i++)
      %pl[%i] = ClientGroup.getObject(%i);
   return %pl[getRandom(0,%i)];
}

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

Random Player:

function getRandomPlayer()
{
   for(%i=0;%i<ClientGroup.getCount();%i++)
      %pl[%i] = ClientGroup.getObject(%i);
   return %pl[getRandom(0,%i)];
}


That is rather inefficient. More optimally:

Code: [Select]
function getRandomPlayer()
{
if ( !( %cnt = clientGroup.getCount() ) )
{
return -1;
}

return clientGroup.getObject( getRandom( 0, %cnt - 1 ) );
}

That is rather inefficient.
Not only inefficient, but may even bring undesired results.

%i will be 1 higher than expected after the loop is done.

Code: [Select]
function getRandomClient()
{
return ClientGroup.getObject(getRandom(0, ClientGroup.getCount() - 1));
}

Code: [Select]
function getRandomClient()
{
return ClientGroup.getObject(getRandom(0, ClientGroup.getCount() - 1));
}
That's the exact same thing as port's, except without the safety check.

Any decent script shouldn't try to get a random client if there are no clients.

Code: [Select]
function getRandomClient()
{
return ClientGroup.getObject(getRandom(0, ClientGroup.getCount() - 1));
}
Any decent script shouldn't try to get a random client if there are no clients.
interesting...
also, would this work?
Code: [Select]
function getRandomClient()
{
    if(clientGroup.getCount())
        return clientGroup.getObject(getRandom(0, clientGroup.getCount() - 1));
    return -1;
}
basicaly, number besides 0 returns true, right?

Yes. Phflack's code is recommended.

Although I cannot think of a case where you would get a random client in an empty server (rationally)

weird city rp lottary every hour? random person gets stuff?

weird city rp lottary every hour? random person gets stuff?
You shouldn't even do that unless there's at least 5 people on the server.

If there's 0 people, the error wont even affect anything anyways.
If there's 1 person, he'll win every time.
If there's 2 people, there's a good chance they're friends and will just sit there and collect. 50% is still too high of a chance.
Etc.

You shouldn't even do that unless there's at least 5 people on the server.

If there's 0 people, the error wont even affect anything anyways.
If there's 1 person, he'll win every time.
If there's 2 people, there's a good chance they're friends and will just sit there and collect. 50% is still too high of a chance.
Etc.
Add some chance for no one to win?

There could be a 15% chance that somebody will be picked randomly.

This also isn't to discuss how you would make your random lottery. I even recommended the one that returns -1 if there's no clients rather than giving an error and returning nothing. (which wont actually produce a different result)