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

    Joined:
    Sep 21, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice