How to make a random that chooses each random selection once
#1

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
Reply
#2

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++;
    }
}
Reply
#3

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

Does no one know how to do it?
Reply
#5

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);
Reply
#6

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.
Reply
#7

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


Forum Jump:


Users browsing this thread: 1 Guest(s)