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
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?
It will be good for us to discuss if you define your problem correctly
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.
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;
}
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;
}
Thanks for your help Salem. You told me exactly what I needed to hear. :cool:
Being nitpicky but should it be "It took ... " :)Code: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:
which requires #include <time.h>Code:srand ( time (NULL) );
Some also do:
Code:srand ( unsigned time (null) );
>srand ( unsigned time (null) );
Then when that doesn't work, they do this:
;)Code:srand((unsigned)time(NULL));
Doh yea forgot the () around unsigned. I never use it so I was going off of old memory.