Thread: Getting three of a kind

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    14

    Getting three of a kind

    I am wondering how to roll three of a kind out of three dice.

    Does each dice have to be random? Would I use a switch?

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    It will be good for us to discuss if you define your problem correctly
    Saravanan.T.S.
    Beginner.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    14
    Roll three dice. Count how many times it takes to get five 3-of-a-kinds.

    That is basically it. I don't have code. The only thing I have written is throwing a pair a thousand times and counting the face value. I am trying a different prog now though.

    I tried a search and came up short.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    14
    I just wrote up something quick based on what you helped me with... am I even headed in the right track?

    Code:
    #include <stdio.h> 
    
    int main(void) 
    
    { 
    
     int three_of_a_kind; 
     int d1;
     int d2;
     int d3;
    
     printf("Rolling three dice: \n\n");
    
    
    d1 = rand() % 6 + 1;
    d2 = rand() % 6 + 1;
    d3 = rand() % 6 + 1;
    if (d1 == d2 && d2 == d3) {
      three_of_a_kind++;
    
     
      
     }   
     printf(" 1%18d\n", d1); 
     printf(" 2%18d\n", d2); 
     printf(" 3%18d\n\n", d3);
     
     return 0; 
    }
    Last edited by Kons; 08-19-2003 at 08:37 AM.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    14
    Ok here is my final product
    Code:
    #include <stdio.h> 
    
    int main(void) 
    
    { 
    
     int three_of_a_kind=0; 
     int d1;
     int d2;
     int d3;
     int iRoll=0;
    
     printf("If you roll three dice: \n\n");
    
     while (three_of_a_kind<5) 
     {
    	iRoll++;
    	d1 = rand() % 6 + 1;
    	d2 = rand() % 6 + 1;
    	d3 = rand() % 6 + 1;
    	if (d1 == d2 && d2 == d3) 
    	{
    	three_of_a_kind++;
    	}   
     }
     printf("It takes %d rolls to get five three-of-a-kinds.\n\n", iRoll);  
     
     return 0; 
    }
    Last edited by Kons; 08-19-2003 at 09:02 AM.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    14
    Thanks for your help Salem. You told me exactly what I needed to hear.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    It takes %d rolls to
    Being nitpicky but should it be "It took ... "

    Also to avoid getting the same results every time, you need to seed the random number generator. The most common way is to do:
    Code:
    srand ( time (NULL) );
    which requires #include <time.h>
    Some also do:
    Code:
    srand ( unsigned time (null) );
    Last edited by Thantos; 08-19-2003 at 09:38 AM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >srand ( unsigned time (null) );
    Then when that doesn't work, they do this:
    Code:
    srand((unsigned)time(NULL));
    My best code is written with the delete key.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Doh yea forgot the () around unsigned. I never use it so I was going off of old memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What kind of job...
    By Discolemonade in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-15-2005, 08:23 PM
  2. what kind compiler should I use
    By ilove in forum C++ Programming
    Replies: 9
    Last Post: 06-22-2005, 05:07 PM
  3. WHat kind of programmer are you?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 02-26-2002, 06:04 PM
  4. What kind of code do u want a front-end designer generating?
    By MovingFulcrum in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-15-2001, 06:45 PM
  5. Physics: Kind of like a cannonball...
    By Cheeze-It in forum Game Programming
    Replies: 11
    Last Post: 09-12-2001, 01:53 PM