Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   rand() help trying to loop it till it finds all numbers in range 4x (http://www.go4expert.com/forums/rand-help-trying-loop-till-range-4x-t23380/)

shredder2500 22Sep2010 01:15

rand() help trying to loop it till it finds all numbers in range 4x
 
what code can i use to make a loop that will find random numbers in the rang of 1 - 13 till all numbers in that range are used 4 times

this is what i would use to just make a loop that finds random numbers

for (int x; x <= 52; x++) // loop till x = 52
{
srand((unsigned)time(NULL)); // seed rand
y += 1 + rand() % (13); //find a random number in the rang of 1 - 13
cout << y << endl; // display random number
}

xpi0t0s 2Oct2010 13:33

Re: rand() help trying to loop it till it finds all numbers in range 4x
 
Here's one way:
Code:

void test30()
{
        int test[13];
        srand((unsigned)time(NULL));
        for (int i=0; i<13; test[i++]=0) ;
        int count=0, p=0;
        while (count<52)
        {
                int r=rand()%13;
                if (test[r]<4)
                {
                        test[r]++;
                        count++;
                        printf("%2d ",r+1);
                        p++;
                        if (p>12)
                        {
                                printf("\n");
                                p=0;
                        }
                }
        }
}



All times are GMT +5.5. The time now is 01:29.