Rolling a Six-Sided Die 6,000,000 Times

To show that the numbers produced by rand occur with approximately equal likelihood, Fig. 6.8 simulates 6,000,000 rolls of a die. Each integer in the range 1 to 6 should appear approximately 1,000,000 times. This is confirmed by the program’s output.

 1   // Fig. 6.8: fig06_08.cpp 2   // Rolling a six-sided die 6,000,000 times. 3   #include <iostream> 4   #include <iomanip> 5   #include <cstdlib> // contains function prototype for rand 6   using namespace std; 7  8   int main() 9   {10      unsigned int frequency1 = 0; // count of 1s rolled11      unsigned int frequency2 = 0; // count of 2s rolled12      unsigned int frequency3 = 0; // count of 3s rolled13      unsigned int ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.