Pick 5 random numbers from array that cant be the same
#1

Hey I need help. I have numbers in an array. I am pulling 5 numbers out of there random. None of the numbers can be the same though. How can I do this? So far I made this:

http://pastebin.com/5KkbHzuZ

But thats doesnt work and it gives some errors on the while() line.

please someone help me
Reply
#2

pawn Код:
stock Give5RandomNumbers(number,&A,&B,&C,&D,&E)
{
    new rand[5];// Create the variables

    rand[0] = random(number);// assign a random value to rand[0] with the max 'number'

    XXXA://declare a position

    rand[1] = random(number); // assign second number

    if(
        rand[0] == rand[1]// check if first value matches second value
    )
        goto XXXA;// if it matches it will go to our position back to pick a new random number
   
    XXXB://and so on....
   
    rand[2] = random(number);

    if(
        rand[0] == rand[2] ||
        rand[1] == rand[2]
    )
        goto XXXB;

    XXXC:
   
    rand[3] = random(number);

    if(
        rand[0] == rand[3] ||
        rand[1] == rand[3] ||
        rand[2] == rand[3]
    )
        goto XXXC;
       
    XXXD:
   
    rand[4] = random(number);

    if(
        rand[0] == rand[4] ||
        rand[1] == rand[4] ||
        rand[2] == rand[4] ||
        rand[3] == rand[4]
    )
        goto XXXD;

    A = rand[0];
    B = rand[1];
    C = rand[2];
    D = rand[3];
    E = rand[4];

    return 1;
}
I do not recommend to use this but this is an exmpale. I have written it now ;P But it should work.
if it stucks in a big loop it wil lag your server, if it stucks in a forever loop your server will need a restart.
Reply
#3

Quote:
Originally Posted by gamer_Z
Посмотреть сообщение
pawn Код:
stock Give5RandomNumbers(number,&A,&B,&C,&D,&E)
{
    new rand[5];// Create the variables

    rand[0] = random(number);// assign a random value to rand[0] with the max 'number'

    XXXA://declare a position

    rand[1] = random(number); // assign second number

    if(
        rand[0] == rand[1]// check if first value matches second value
    )
        goto XXXA;// if it matches it will go to our position back to pick a new random number
   
    XXXB://and so on....
   
    rand[2] = random(number);

    if(
        rand[0] == rand[2] ||
        rand[1] == rand[2]
    )
        goto XXXB;

    XXXC:
   
    rand[3] = random(number);

    if(
        rand[0] == rand[3] ||
        rand[1] == rand[3] ||
        rand[2] == rand[3]
    )
        goto XXXC;
       
    XXXD:
   
    rand[4] = random(number);

    if(
        rand[0] == rand[4] ||
        rand[1] == rand[4] ||
        rand[2] == rand[4] ||
        rand[3] == rand[4]
    )
        goto XXXD;

    A = rand[0];
    B = rand[1];
    C = rand[2];
    D = rand[3];
    E = rand[4];

    return 1;
}
I do not recommend to use this but this is an exmpale. I have written it now ;P But it should work.
if it stucks in a big loop it wil lag your server, if it stucks in a forever loop your server will need a restart.
Yeah could of done this but I really didn't want to make something this big.
Reply
#4

bigger doesn't mean always slower xD
And when I look at your code your chance tostuck in an infite loop is bigger and the code is much slower.
Reply
#5

I made such a thing a while back. You'll need to adapt it, but it'll give you an idea on how to start.

pawn Код:
public GenerateHorseBet()
{
    new
        count = 0,
        previousnum = -1,
        currentnum = -1;

    previousnum = random(sizeof(eHorseNames));

    Horses[0] = previousnum;
    count++;

    while(count != 5)
    {
        do
        {
            currentnum = random(sizeof(eHorseNames));
            for(new i = 0; i < 5; i++)
            {
                if(Horses[i] == currentnum)
                {
                    previousnum = currentnum;
                }
            }
        }
        while(previousnum == currentnum); // if they are the same, choose a new number

        Horses[count] = currentnum;
        previousnum = currentnum;
        count++;
    }
    /*
    printf("[info] Chosen horses for this bet:\n %s (%d)\n %s (%d)\n %s (%d)\n %s (%d)\n %s (%d)",
    eHorseNames[Horses[0]], Horses[0],
    eHorseNames[Horses[1]], Horses[1],
    eHorseNames[Horses[2]], Horses[2],
    eHorseNames[Horses[3]], Horses[3],
    eHorseNames[Horses[4]], Horses[4]);
    */

    return 1;
}
Reply
#6

Thanks Vince, and everyone else.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)