# rolling dice program

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 03-21-2005
itsme86
Your results make sense too:
Code:

```itsme@dreams:~/C\$ cat funky.c #include <stdio.h> int main(void) {   unsigned char num;   double fval;   int ival;   for(num = 0;num < 8;++num)   {     fval = ((double)(num & 0x7) / 8 * 6) + 1;     ival = fval;     printf("num: %d, fval = %f, ival = %d\n", num, fval, ival);   }   return 0; }```
Code:

```itsme@dreams:~/C\$ ./funky num: 0, fval = 1.000000, ival = 1 num: 1, fval = 1.750000, ival = 1 num: 2, fval = 2.500000, ival = 2 num: 3, fval = 3.250000, ival = 3 num: 4, fval = 4.000000, ival = 4 num: 5, fval = 4.750000, ival = 4 num: 6, fval = 5.500000, ival = 5 num: 7, fval = 6.250000, ival = 6```
You can see why 1 and 4 get twice as many.
• 03-21-2005
itsme86
I'm kind of mathematically retarded, but it seems like this is a problem when getting any random number out of a computer that has a limit of RAND_MAX-1 or smaller. It seems like the "number favoritism" would be more prounounced the closer the upper limit gets to RAND_MAX. Can someone please correct me if I'm wrong?
• 03-21-2005
sunny2005
dice
the only thing i need to do is i need to wite a program in C, which simulates the rolling of two dice. the program shiuold use rand to roll the first die and use rand again to roll second die. the sum shukld tehn be calculated. the progms shuld roll the two dice 36000 times. i need to writ eit in simple c. so if csomeone can help me out it will be greatly apprecaited. thnks .
• 03-21-2005
Dave_Sinkula
Quote:

Originally Posted by itsme86
I'm kind of mathematically retarded, but it seems like this is a problem when getting any random number out of a computer that has a limit of RAND_MAX-1 or smaller. It seems like the "number favoritism" would be more prounounced the closer the upper limit gets to RAND_MAX. Can someone please correct me if I'm wrong?

http://www.eskimo.com/~scs/C-faq/q13.16.html
Quote:

Both methods obviously require knowing RAND_MAX (which ANSI #defines in <stdlib.h>), and assume that N is much less than RAND_MAX.
• 03-21-2005
sunny2005
the only thing i need to do is i need to wite a program in C, which simulates the rolling of two dice. the program shiuold use rand to roll the first die and use rand again to roll second die. the sum shukld tehn be calculated. the progms shuld roll the two dice 36000 times. i need to writ eit in simple c. so if csomeone can help me out it will be greatly apprecaited. thnks .
• 03-21-2005
Dave_Sinkula
Quote:

Originally Posted by sunny2005
the only thing i need to do is i need to wite a program in C, which simulates the rolling of two dice. the program shiuold use rand to roll the first die and use rand again to roll second die. the sum shukld tehn be calculated. the progms shuld roll the two dice 36000 times. i need to writ eit in simple c. so if csomeone can help me out it will be greatly apprecaited. thnks .

You've yet to post an attempt. Here's a start.
Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> int foo(int lo, int hi) {   return /* fill in with rand() & stuff */0; } int main(void) {   int i;   srand(time(0));   for ( i = 0; i < 36000; ++i )   {       int die1 = foo(1, 6);       int die2 = foo(1, 6);       int sum = die1 + die2;   }   return 0; }```
Think a little -- show some effort, or prepare to be ignored.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12