SA-MP Forums Archive
How to make a random that chooses each random selection once - 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: How to make a random that chooses each random selection once (/showthread.php?tid=464264)



How to make a random that chooses each random selection once - Mennims - 16.09.2013

I need help with a script, I have a random
pawn Code:
new RandomPutPlayerInVehicle[][1] =
{
    {1},
    {2},
    {3},
    {4},
    {5},
    {6},
    {7},
    {8},
    {9},
    {10},
    {11},
    {12},
    {13},
    {14},
    {15},
    {16},
    {17},
    {18},
    {19},
    {20},
    {21},
    {22},
    {23},
    {24},
    {25},
    {26},
    {27},
    {28},
    {29},
    {30},
    {31},
    {32},
    {33},
    {34}
}
and I need it to choose each of these ONCE! So I don't have a duplicate. Please help


Re: How to make a random that chooses each random selection once - Bakr - 16.09.2013

Firstly, that is a very poor array structure. It only needs to be on dimension.

Secondly, I would use a method similar to this:
pawn Code:
while( ( id = RandomPutPlayerInVehicle[ random( sizeof( RandomPutPlayerInVehicle ) ) ] ) != -1)
    RandomPutPlayerInVehicle[ id - 1 ] = -1;
Since the values in the array are basically the index plus one, you could just set the value at that index to -1 once it is used. The above code will assign id a value at a random index in the array, and will continue to do so if the random number chosen happens to be used already (-1). Once that slot is then chosen and successfully, finally, assigned to id, it sets that index to used (-1). Note that this will only work while the values in the array are each in relation to the index in some way.

For anyone who cares, here is the test code (might make it easier to understand the concept):
pawn Code:
new test[] = { 1, 2, 3, 4, 5 };

main() {
    new i;
    new id;
    while( i < 5 ) {
        while( ( id = test[ random( sizeof( test ) ) ] ) != -1 ) {
                test[ id - 1 ] = -1;
                printf( "Slot %i", id - 1 );
        }
        i++;
    }
}



Re: How to make a random that chooses each random selection once - Mennims - 16.09.2013

That didn't help at all. Please give me an example and explain it. Not just dump it


Re: How to make a random that chooses each random selection once - Mennims - 16.09.2013

Does no one know how to do it?


Re: How to make a random that chooses each random selection once - Dragonsaurus - 16.09.2013

Easy, just search if there is noone at the vehicle used before and reloop.
pawn Code:
new veh;
randomcar:veh = RandomPutPlayerInVehicle[random(sizeof(RandomPutPlayerInVehicle))];
for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(IsPlayerInVehicle(i, veh)) goto randomcar;
}
PutPlayerInVehicle(playerid, veh, 0);



Re: How to make a random that chooses each random selection once - Bakr - 16.09.2013

Quote:
Originally Posted by Mennims
View Post
That didn't help at all. Please give me an example and explain it. Not just dump it
That is exactly what I did. Just because you are incapable of understanding the basic implementation doesn't mean you can brush it off as if there was no information.


Re: How to make a random that chooses each random selection once - Mennims - 17.09.2013

Thanks Dragonsaurus, but I can't rep you, I still need to spread rep, even though I have I will rep when I can