Selecting x number of unique random values from an array
#1

Just wondering what the most efficient way of doing this is, because the only thing I can think of involves many loops.

I have an array with ~100 strings in. I want to randomly select 10 of them, without having the same one twice. What's the best way to do this?
Reply
#2

pawn Код:
stock GetRandomRows(values[], amount, max_values = sizeof(values))
{
    if(max_values <= amount && values[0] == -1)
    {
        new i, i2, rand;
        while(i < max_values)
        {
            rand = random(amount);
            i2 = 0;
            while(i2 < max_values)
            {
                if(values[i2] == rand)
                {
                    rand = random(amount);

                    i2 = 0;
                    continue;
                }

                i2++;
            }

            values[i] = rand;
            i++;
        }
       
        return 1;
    }

    return 0;
}
pawn Код:
new values[10] = { -1, ... };
GetRandomRows(values, sizeof(strings_array));
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)