// // Program name: roll_dice_v1 // Author: B. Massingill // // Input: none // Output: (on standard output) how many rolls it takes to see all // 6 faces of a die, trying one random sequence of rolls // #include #include int main() { bool faces[6] ; // faces[i] is true if we've // seen face i int rolls = 0 ; // number of rolls so far int numFacesSeen = 0 ; // number of distinct faces // seen so far int faceThisRoll ; // face displayed by current // roll // initialize faces array int i = 0 ; while (i < 6) { faces[i] = false ; i++ ; } // now roll the die repeatedly until we've seen all faces while (numFacesSeen < 6) { // "roll the die": generate a random number between // 0 and 5 faceThisRoll = 1 + (int) ((6.0 * rand())/(RAND_MAX + 1.0)) ; rolls += 1 ; // uncomment the next line to see the results of // each roll // cout << "the number is " << faceThisRoll << endl ; if (!faces[faceThisRoll - 1]) { // face we haven't seen before faces[faceThisRoll - 1] = true ; numFacesSeen += 1 ; } } cout << rolls << " rolls were required\n" ; return 0 ; }