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?

Printable View

- 08-19-2003KonsGetting 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? - 08-19-2003saravanan_ts
It will be good for us to discuss if you define your problem correctly

- 08-19-2003Kons
**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. - 08-19-2003Kons
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;

}

- 08-19-2003Kons
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;

}

- 08-19-2003Kons
Thanks for your help Salem. You told me exactly what I needed to hear. :cool:

- 08-19-2003ThantosCode:
`It takes %d rolls to`

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) );`

Some also do:

Code:`srand ( unsigned time (null) );`

- 08-19-2003Prelude
>srand ( unsigned time (null) );

Then when that doesn't work, they do this:

Code:`srand((unsigned)time(NULL));`

- 08-19-2003Thantos
Doh yea forgot the () around unsigned. I never use it so I was going off of old memory.