rolling dice program

This is a discussion on rolling dice program within the C Programming forums, part of the General Programming Boards category; : hi. im trying to work on a program our teach gave us. i need to wite a program in ...

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

    Question rolling dice program

    : hi. im trying to work on a program our teach gave us. 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. we have to use standard librairies, we are jsut learning C . so if ANYONE knows how to do this, or knows if there is a place where i can get help, please do tell! thanks you some, pelse do respond. please, this is worth 150 points and i only have 2 progects. adn this is our first one, i ahve one mroe some otehr time. but this will really count! plesae thank u . i jsut dont know how to do this , if someone can help me , it will be greatly appreciated , thank you.

    i think this is the algorithm so far :

    : : initialize an int array with 36000 elements if you need to store them, otherwise declare a single int.
    : : seed the generator using srand
    : : while an array element is empty or if no array just let it loop 36000 times(a for loop will work great here, but any will do)
    : : -get random number and put it in the current array element or the int
    : : - if you need to print it, use printf() to print it out.

    but i dont know how to use srand and rand. some pelase HELP.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    That's interesting...I'm working on something similar right now. The first thing I did was looked at the FAQ. This link covers most of the basics:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    I prefer this one, though. I forgot exactly what was wrong with using the modulus operator to generate random numbers, but Prelude reminded me:
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    From the second link:
    The first thing to realize is that computer generated random numbers are not random. Speaking strictly abstract, there is no such thing as a random number, but realistically a random number is a sequence of independent values with a specific distribution (ie. obtained by chance and having nothing to do with the other numbers in the sequence).
    If you're on the right system with the right needs, the beginning of the above statement isn't true:
    http://bama.ua.edu/cgi-bin/man-cgi?urandom+7D

    But anyway, in responese the OP's problem, what's the purpose of the 36000-element array? Do you need a history of the sums for any reason? I didn't see that requirement stated.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Additionally, the second link says that the following code should return a number from 1 to 10:
    Code:
    (int)( (double)std::rand() / ( RAND_MAX + 1 ) * 10 )
    Actually, it produces numbers from 0 to 9. If you're looking for random numbers in a range, try this:
    Code:
    int random(int low, int high)
    {
    	return ((int)((double)std::rand() / (RAND_MAX+1) * (high-low+1)) + low);
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If you're on the right system with the right needs, the beginning of the above statement isn't true:
    http://bama.ua.edu/cgi-bin/man-cgi?urandom+7D
    While your link does mention strong random numbers I saw nothing in the skimming to suggest a true random order. If you want to get philosophical there is nothing in life that is random.

    RAND_MAX + 1
    This is just begging for an overflow.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Heh, I'm game.
    Quote Originally Posted by Thantos
    RAND_MAX + 1
    This is just begging for an overflow.
    Why? rand() returns an int, so RAND_MAX can be no larger than an int. RAND_MAX+1 might not be an int, but it should fit in a long or an unsigned int. The compiler should know what to do with it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by pianorain
    Heh, I'm game.Why? rand() returns an int, so RAND_MAX can be no larger than an int. RAND_MAX+1 might not be an int, but it should fit in a long or an unsigned int.
    And if a long has the same range as an int? And if RAND_MAX == INT_MAX?
    Quote Originally Posted by pianorain
    The compiler should know what to do with it.
    The programmer should know what s/he is doing.
    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.*

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    All right, sounds good. So how about the following?
    Code:
    (unsigned int)RAND_MAX + 1
    That shouldn't overflow. RAND_MAX cannot be larger than INT_MAX, since rand() only returns an int. Hence, RAND_MAX cannot be equal to UINT_MAX. Am I making some unwarrented assumptions?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    How about double, since that's where you're going anyway?
    Code:
    int result = rand() / (RAND_MAX + 1.0) * N;
    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.*

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    Since there's no way the OP would try to submit this has his own program, I figured I'd post it. I just whipped it up for fun:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void roll(int *dice, unsigned char num)
    {
      dice[0] = ((num & 0x7) % 6) + 1;
      dice[1] = (((num >> 3) & 0x7) %6) + 1;
    }
    
    void roll_dice_urandom(int *dice, FILE *fp)
    {
      int num;
    
      if((num = fgetc(fp)) == EOF)
      {
        puts("Error reading from urandom!");
        exit(EXIT_FAILURE);
      }
    
      roll(dice, (unsigned char)num);
    }
    
    void roll_dice_rand(int *dice, FILE *fp)
    {
      roll(dice, (unsigned char)rand());
    }
    
    void show_bar(int len)
    {
      while(len--)
        putchar('X');
      putchar('\n');
    }
    
    int main(int argc, char **argv)
    {
      int dice[2];
      int *sums = NULL;
      int nrolls;
      int i;
      FILE *fp = NULL;
      void (*roll_dice)(int *, FILE *);
      int frequency[11] = { 0 };
    
      if(argc < 2 || argc > 3)
      {
        printf("Usage: %s <number of rolls> [r]\n", argv[0]);
        puts("The program tries to use /dev/urandom for random numbers."
             " If it can't, it will fall back on rand(). Specifying 'r'"
             " on the command lind overrides normal behavior and forces"
             " the use of rand() even if /dev/urandom is available.");
        exit(EXIT_FAILURE);
      }
    
      if((nrolls = atoi(argv[1])) < 1)
      {
        puts("Number of rolls must be greater than 0");
        exit(EXIT_FAILURE);
      }
    
      if(argc == 3)
      {
        if(strcmp(argv[2], "r"))
        {
          puts("Invalid command line option.");
          exit(EXIT_FAILURE);
        }
    
        srand(time(NULL));
        roll_dice = roll_dice_rand;
      }
      else
      {
        if(!(fp = fopen("/dev/urandom", "r")))
        {
          srand(time(NULL));
          roll_dice = roll_dice_rand;
        }
        else
          roll_dice = roll_dice_urandom;
      }
    
      if(!(sums = malloc(sizeof(int) * nrolls)))
      {
        puts("Memory allocation error!");
        exit(EXIT_FAILURE);
      }
    
      for(i = 0;i < nrolls;++i)
      {
        roll_dice(dice, fp);
        sums[i] = dice[0] + dice[1];
      }
    
      if(fp)
        fclose(fp);
    
      for(i = 0;i < nrolls;++i)
        frequency[sums[i] - 2]++;
    
      printf("Sums%s:\n", nrolls > 50 ? " (first 50)" : "");
      for(i = 0;i < (nrolls > 50 ? 50 : nrolls);++i)
        printf("%d ", sums[i]);
      putchar('\n');
    
      puts("\nFrequency:");
      for(i = 0;i < 11;++i)
        printf("%d:%d  ", i + 2, frequency[i]);
      putchar('\n');
    
      for(i = 0;i < 11;++i)
      {
        printf("%2d ", i + 2);
        show_bar((int)(((double)frequency[i] / nrolls) * 75));
      }
    
      free(sums);
    
      return EXIT_SUCCESS;
    }
    Code:
    itsme@dreams:~/C$ ./dice 36000
    Sums (first 50):
    7 8 6 6 8 10 8 11 2 6 6 3 11 5 6 5 5 3 4 8 6 8 6 6 3 7 5 9 8 4 5 7 5 7 6 9 6 11 6 6 6 4 9 6 6 9 9 8 2 11
    
    Frequency:
    2:2342  3:4418  4:4403  5:4438  6:5149  7:5693  8:3882  9:2324  10:1669  11:1130  12:552
     2 XXXX
     3 XXXXXXXXX
     4 XXXXXXXXX
     5 XXXXXXXXX
     6 XXXXXXXXXX
     7 XXXXXXXXXXX
     8 XXXXXXXX
     9 XXXX
    10 XXX
    11 XX
    12 X
    itsme@dreams:~/C$
    EDIT: My roll() function might be a really poor way to get proper randomness, because those results look suspicious to me.

    EDIT 2: At least I'm getting suspicious results consistently! I ran the program with the same parameters at least a dozen times and the graph at the end looks identical every time, the sum frequencies are so close to the same on every run.
    Last edited by itsme86; 03-21-2005 at 11:25 AM.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    There's definitely something wrong with my method of deciding dice. If I keep track of the individual die frequency 1 and 2 come up twice as often 3, 4, 5, and 6:

    On a run of 1000000 rolls.
    Code:
    Die Frequency:
    1:500054 2:499915 3:248935 4:250437 5:249819 6:250840
    I'm going to try to figure out why this happens, but if any of you see it right away, please let me know
    If you understand what you're doing, you're not learning anything.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Why not get a random value between 1-6 for each die rather than getting a single random number to determine the result of both dice?
    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.*

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    Nevermind, I'm retarded. Of course 1 and 2 come up twice as often. For each die I'm pulling 3 bits of the number giving me possible 0 through 7. I add 1 to that which gives me 1 through 8. And then I just assign the die with the remainder of that number divided by 6, which means the die ends up being 1 or 2 if the original number was any of 1, 2, 7, or 8. I just need a better method
    Last edited by itsme86; 03-21-2005 at 12:57 PM.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    Quote Originally Posted by Dave_Sinkula
    Why not get a random value between 1-6 for each die rather than getting a single random number to determine the result of both dice?
    Just to be cute
    If you understand what you're doing, you're not learning anything.

  15. #15
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by itsme86
    I just need a better method
    I tried a method similar to what is suggested in the FAQ:
    Code:
    void roll(int *dice, unsigned int num)
    {
    	dice[0] = (int)((double)(num & 0x7) / 8 * 6) + 1; 
    	dice[1] = (int)((double)((num >> 3) & 0x7) / 8 * 6) + 1;
    }
    Surprisingly, this output the following:
    Code:
    1: 49831
    2: 25079
    3: 24904
    4: 50025
    5: 24809
    6: 25352
    Now 1 and 4 are both high. I can't say I understand why.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Page 1 of 2 12 LastLast
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, 10: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, 06:49 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21