Use vector to track Dice Rolling random number - C++ STL

C++ examples for STL:vector

Description

Use vector to track Dice Rolling random number

Demo Code

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <vector>

int rollDie();//from ww w .j  a v a  2  s  .com

static int totalRolls = 36000;
static size_t total = 13;

int main(int argc, const char *argv[]) {
    std::vector<int> tally(total);

    srand(time(0));

    for (int i = 0; i < totalRolls; ++i) {
        ++tally[rollDie() + rollDie()];
    }

    for (unsigned int i = 2; i < total; ++i) {
        std::cout << std::setw(2) << i << ": " << tally[i] << std::endl;
    }

    return 0;
}
// roll a single die
int rollDie() { 
   return rand() % 6 + 1; 
}

Result


Related Tutorials