Using Function srand to Randomize the die-rolling program - C++ Data Type

C++ examples for Data Type:Random

Description

Using Function srand to Randomize the die-rolling program

Demo Code

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

int main(int argc, const char *argv[]) {
    unsigned seed;

    std::cout << "Enter seed: ";
    std::cin >> seed;//from w w  w.  j  a  va2 s. c om

    srand(seed);

    for (int counter = 0; counter <= 10; ++counter) {
        std::cout << std::setw(10) << (1 + rand() % 6);

        if (counter % 5 == 0) std::cout << std::endl;
    }
    return 0;
}

Result


Related Tutorials