Thread: dice rolling in c

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    dice rolling in c

    Ok, I want to just say that this dice rolling program has been a pain in the .......... from the beginning, ok -- moving on.

    I want to know if anyone at all could give me a hint as to how to find the average of multiple simulated rolls in a dice program. I know that a single roll has a 3.5 average and for two dice you multiply that times two... but what if I wanted to find the average of the sum for each of my 100000 random rolls?

    Disclaimer: I know this is wrong, but....

    This is what I thought of: 3.5 x 2 = 7 x 100000 = 700000 / 100000 = 7 ? ok, bare with me.

    I also know that there is a 1/36 probability of rolling any number on the die. Maybe I should use this probability in the equation?

    Code:
    printf("\n\nPlease input a number for the seed value: ");   //User needs to input a seed value
     
    	scanf("%f", &seed);
    	
    	
    	srand( seed );											     //implementation of the random # generator function
     
    	for ( count = 0; count < 11; count++ )					     // An array that makes sure the proper syncing of elements 
    														         // takes place.
    			rolls[ count ] = 0; 
     
     
    	for ( count = 1; count <= 100000; count++ ) {			     //A counter that calls the 'roleDie' funtion
     
    			twoDice = roleDie() + roleDie();
    			++ rolls[ twoDice - 2 ];
     }
     
     //printf("\nthis is twodice %d\n",twoDice);
     
     //average = ( twoDice / 100000.0); 
    
    	printf("\n\nThe number of times the simulated roll of dice was 2: %d\n\n", rolls[0]);
     
    	printf("The number of times the simulated roll of dice was 7: %d\n\n", rolls[5]);
     
    	printf("The nuber of times the simulated roll of dice was 12: %d\n\n", rolls[10]);
     
    	printf("The average of all simultaneous dice rolls is: %.3f", average);
    	
     
    	}
    
    
    
    
    
    //*********************************
    //  USER DEFINED FUNCTION: roleDie
    //     'Random Number Generator'
    //*********************************
    
    int roleDie() {
    
     float result;
     int  roll;
     
     result = rand();
     roll = (result * 6) / 32768;
     
     return( roll + 1 );
    }
    Ok guys, so you see the code. Can anyone clue me in?
    Last edited by Salem; 11-06-2010 at 11:12 AM. Reason: fixed code tags

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok a few things....

    Your best random seed is time(0) ... srand(time(0));
    Asking for user input is likely to result in many repititions of the same sequence. (Hint, most people will enter a single digit number)

    The actual dice roll is not accurately represented in your rand function. You could simplify that down to simply say return (rand() % 6) + 1;

    The average is pretty much the sum of all values from rolling the dice divided by the number of rolls... So you're on the right track by simply adding them up and dividing.
    Last edited by CommonTater; 11-06-2010 at 11:26 AM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    Great, thanks commontater

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're over-thinking this!

    Roll your dice: in a loop, call the roll dice function. Send it a sum (long unsigned int), to add up all the rolls that are made, which has been initialized to zero. Be sure it's handled as a call by reference, not by value (give it an address, receive it as a pointer, so the number can be changed).

    (or you can return the sum via a function return if you want).

    When all the rolls have been made, cast the unsigned long as a (double), , and divide it by the number of rolls made. Be sure the final average is not truncated by being handled as an int.

    Simulated != simultaneous.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Designing Data Structures for Dice Rolling
    By drunken_scot in forum C Programming
    Replies: 6
    Last Post: 02-27-2009, 01:01 AM
  2. Replies: 9
    Last Post: 11-11-2007, 02:31 PM
  3. Dice Rolling Program help
    By courtesan in forum C Programming
    Replies: 3
    Last Post: 08-17-2005, 03:14 PM
  4. Rolling Dice
    By pianorain in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-22-2005, 07:07 AM
  5. rolling dice program
    By sunny2005 in forum C Programming
    Replies: 20
    Last Post: 03-21-2005, 04:45 PM

Tags for this Thread