Selecting x number of unique random values from an array - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Selecting x number of unique random values from an array (
/showthread.php?tid=421485)
Selecting x number of unique random values from an array -
MP2 - 09.03.2013
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?
Re: Selecting x number of unique random values from an array -
Finn - 09.03.2013
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));