Thread: rolling dice program

  1. #16
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  2. #17
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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?
    If you understand what you're doing, you're not learning anything.

  3. #18
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Question 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 .

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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
    Both methods obviously require knowing RAND_MAX (which ANSI #defines in <stdlib.h>), and assume that N is much less than RAND_MAX.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    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 .

  6. #21
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM