Pick 4 random numbers from array.
#1

Hi,
How to pick 4 random numbers from array and not repeat?
pawn Код:
new arrVehRandCP[28][1] =
{
      411,
      424,
      413,
      489,
      589,
      477,
      404,
      561,
      493,
      470,
      452,
      562,
      486,
      571,
      558,
      527,
      426,
      402,
      534,
      480,
      551,
      492,
      400,
      567,
      575,
      466,
      412,
      418
};
Thanks.
Reply
#2

pawn Код:
PickNumbers(array[], s_size = sizeof(array))
{
    static arrVehRandCP[] =
    {
          411,
          424,
          413,
          489,
          589,
          477,
          404,
          561,
          493,
          470,
          452,
          562,
          486,
          571,
          558,
          527,
          426,
          402,
          534,
          480,
          551,
          492,
          400,
          567,
          575,
          466,
          412,
          418
    };

    if(s_size > sizeof(arrVehRandCP))
        s_size = sizeof(arrVehRandCP);

    new TemparrVehRandCP[sizeof(arrVehRandCP)], rnd;
    TemparrVehRandCP = arrVehRandCP;

    while(s_size > 0)
    {
        rnd = random(sizeof(arrVehRandCP));
        if(TemparrVehRandCP[rnd])
        {
            array[--s_size] = TemparrVehRandCP[rnd];
            TemparrVehRandCP[rnd] = 0;
        }
    }
}
Usage:
pawn Код:
new My_Number[4];
PickNumbers(My_Number);

for(new i=0; i < sizeof(My_Number); i++)
    printf("%d. %d",i,My_Number[i]);
Reply
#3

Thank you, It is perfect .

When players enter the checkpoint :
pawn Код:
new Veh = GetPlayerVehicleID(playerid);
new VehModel = GetVehicleModel(Veh);
if(My_Number[i] == VehModel)
{
      // <===== I want only the My_Number[i] again to be random.
      SendClientMessage(playerid,-1,"Test.");
      return 1;
}
Thanks .
Reply
#4

anyone ?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)