1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <random>
#include <ctime>
#include <iomanip>
int main()
{
const int NROLLS = 6'000'000 ;
const int MIN_VALUE = 1 ;
const int MAX_VALUE = 6 ;
const int ARRAY_SZ = MAX_VALUE + 1 ;
// http://en.cppreference.com/w/cpp/numeric/random
std::mt19937 rng( std::time(nullptr) ) ; // random number engine (seeded with current time)
std::uniform_int_distribution<int> die( 1, 6 ) ; // fair die
// frequencies of 1, 2, 3, 4, 5, 6 frequency_count[0] is not used
int frequency_count[ARRAY_SZ] {} ; // initialise to all zeroes
// readable tags for 1, 2, 3, 4, 5, 6 "zeroes" is not used
const char* frequency_name[ARRAY_SZ] { "zeroes", "ones", "twos", "threes", "fours", "fives", "sixes" } ;
// roll the die NROLLS times and update frequencies
for( int n = 1 ; n < NROLLS ; ++n ) ++frequency_count[ die(rng) ] ;
// print the results
for( int i = MIN_VALUE ; i < ARRAY_SZ ; ++i )
std::cout << "you rolled " << std::setw(7) << frequency_count[i] << ' ' << frequency_name[i] << '\n' ;
}
|