Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

A program to simulate the rolling of 2 dice, 1000 times


I'm trying to simulate two die rolling 1000 times, but once the results are in its always in favor of 2 numbers (for example, it sometimes goes 600 on a number then 400 on another). I'm new to programming so I'd appreciate any help, i don't really know what I'm doing wrong.

C++
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int ofTwo, ofThree, ofFour, ofFive, ofSixth, ofSeven, ofEight, ofNine, ofTen, ofEleven, ofTwelve, sum, numOfTime = 0;


int dieroll(void) 
{
    int ran;
	    srand(time(NULL));
	   int min = 1; // the min number a die can roll is 1
       int max = 6;// this->dieSize; // the max value is the die size

        ran = rand() % (max - min + 1) + min;
	    std::cout << "You rolled a " << std::endl;
        std::cout << "_______" << std::endl;
        std::cout << "|||||||" << std::endl;
        std::cout << "|||" << ran << "|||" << std::endl;
        std::cout << "|||||||" << std::endl;
        std::cout << "-------" << std::endl;
	return ran;
}


int dice(int fdie, int sdie) 
{
    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << "Your total so far is: " << sdie + fdie << std::endl;
    return sdie + fdie;
}

int results(int total) 
{
		std::cout << "You may continue to roll." << std::endl << std::endl;
    if(numOfTime == 1000)
    {
        return 0;
    }
		return 1;
}

// Main Funtion
int main(void) 
{
	int counter, total, seconddie, firstdie;
	char secondstart;
	
        
      for(int i = 0; i < 1000; i++)
      {
        firstdie = dieroll();
	   	seconddie = dieroll();
        total = dice(firstdie, seconddie);
          numOfTime++;
         if(total == 2)
            ofTwo++;
        if(total == 3)
            ofThree++;
        if(total == 4)
            ofFour++;
        if(total == 5)
            ofFive++;
        if(total == 6)
            ofSixth++;
        if(total == 7)
            ofSeven++;
        if(total == 8)
            ofEight++;
        if(total == 9)
            ofNine++;
        if(total == 10)
            ofTen++;
        if(total == 11)
            ofEleven++;
        if(total == 12)
            ofTwelve++;
      }
        
       cout << endl;
       cout << endl;
       cout << endl;
       cout << endl;
       cout << endl;
    

   
     std::cout << "A 2 was rolled " << ofTwo << " times" << std::endl;
     std::cout << "A 3 was rolled " << ofThree << " times" << std::endl;
     std::cout << "A 4 was rolled " << ofFour << " times" << std::endl;
     std::cout << "A 5 was rolled " << ofFive << " times" << std::endl;
     std::cout << "A 6 was rolled " << ofSixth << " times" << std::endl;
     std::cout << "A 7 was rolled " << ofSeven << " times" << std::endl;
     std::cout << "A 8 was rolled " << ofEight << " times" << std::endl;
     std::cout << "A 9 was rolled " << ofNine << " times" << std::endl;
     std::cout << "A 10 was rolled " << ofTen << " times" << std::endl;
     std::cout << "A 11 was rolled " << ofEleven << " times" << std::endl;
     std::cout << "A 12 was rolled " << ofTwelve << " times" << std::endl;
    
	
	std::cin.ignore();
	return 0;
}


What I have tried:

I've tried using "do, while" but its the same result.
Posted
Updated 3-May-22 10:11am
v2

The problem is your use of srand(). srand() resets the random number generator using the given seed. So if you call srand(10), for example, then the next call to rand() will always be the same value, maybe 19320. So if you have a loop like this
C++
for(int i = 0; i < 10; ++i) {
   srand(time(NULL));
   std::cout << rand() << '\n';
}
You will get the same number printed 10 times. What you wan to do is to call srand() once only, probably in your main() routine.
As a side note, the old rand() function has some deficiencies that make it unsuitable for any serious random number usage. Far better is the newer C++ Pseudo-random number generation[^]
 
Share this answer
 
Comments
CPallini 4-May-22 2:01am    
5.
Every time you call dieroll, you reinitialize the random number generator by calling srand. This will often cause it to generate the same number many times in a row. You should only call srand once, when your program starts up.

Totally unrelated to that, you should use an array to store your results. int freq[13] would be fine, which allows you to index freq[2] through freq[12] using total.
 
Share this answer
 
Comments
CPallini 4-May-22 2:01am    
5.
HardCoreHam 4-May-22 13:31pm    
Thank you very much
Greg Utas 4-May-22 13:33pm    
You're welcome.
Quote:
i don't really know what I'm doing wrong.

With C/C++, you are responsible of every thing, including setting initial value of variables !
C++
int ofTwo = 0, ofThree = 0, ofFour = 0, ofFive = 0, ofSixth = 0, ofSeven = 0, ofEight = 0, ofNine = 0, ofTen = 0, ofEleven = 0, ofTwelve = 0, sum = 0, numOfTime = 0;

More same problem in your code.
 
Share this answer
 
v2
Comments
CPallini 4-May-22 2:02am    
5.
Patrice T 4-May-22 2:44am    
Thank you
I think the previous solutions showed you what needs to happen. I would make a few modifications. First, this is what I would do for the function to roll the dice :
C++
int RollDie() 
{
   int value = rand() % 6;    // value will range from 0 to 5
   ++value;                   // add one so it ranges from 1 to 6
   std::cout << "You rolled a " << value << std::endl;
   return value;
}
Second, I would change your counters to be an array because that will simplify things a LOT. Since the total of two dice can range from 2 to 12 we will use an array with 13 items and the first two items will not be used, indexes 0 and 1. Here's how that might look :
C++
int main()
{
    const int maxrolls = 1000;
    const int mindice = 2;
    const int maxdice = 12;

    // the array size has +1 so 12 is a valid index

    int totals[ maxdice + 1 ] = { 0 };     // initialized to all zeroes
    int first;
    int second;
    int sum;
    for(int i = 0; i < maxrolls; i++)
    {
        first = RollDie();
        second = RollDie();
        sum = first + second;
        totals[ sum ] += 1;                // increment total for this roll
    }

    // display totals - the loop will run from 2 to 12, inclusively because of <=

    for( int n = mindice; n <= maxdice; ++n )
        std::cout << "A " << n << " was rolled " << totals[ n ] << " times" << std::endl;

}
 
Share this answer
 
Comments
CPallini 4-May-22 2:03am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900