Random player from table

Started by Pejson, Dec 22, 2016, 09:24 AM

Previous topic - Next topic

Pejson


jWeb

Search the forum, there's plenty of information.

http://forum.vc-mp.org/?topic=180.msg921#msg921

Also, you can't select random stuff from tables because that's not how tables work.

EK.IceFlake

Untested
RandomFromTable(tbl)
{
    local tarr;
    foreach (item in tbl) tarr.push(item);
    return tarr[Random(0, tarr.len() - 1)];
}
Use this: http://forum.vc-mp.org/?topic=246.0

jWeb

No need to create a copy of the table just to select a random value from it:
::srand((::GetTickCount() % ::time()) / 3);

RandomFromTable(tbl)
{
    local ridx = ::rand() % tbl.len(), idx = 0;

    foreach (k, v in tbl)
    {
        if (idx == ridx)
        {
            return v;
        }

        ++idx;
    }
}