1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

rand() help trying to loop it till it finds all numbers in range 4x

Discussion in 'C' started by shredder2500, Sep 21, 2010.

  1. shredder2500

    shredder2500 New Member

    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
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    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;
    			}
    		}
    	}
    }
    
     

Share This Page