So I'm writing this program that let you roll a dice and show you the random number, but I want the program to show how many 1,2,3 etc that you get when you roll. For example, You rolled '4' ones. You rolled '3' twos etc. This is the code I have this far. I'm thinking that maybe a if-statement would work?
#include <iostream>
usingnamespace std;
void dice(int nr);
int main ()
{
int num;
cout << "How many times do you want to roll the dice? ";
cin >> num;
dice(num);
cout << endl;
return 0;
}
void dice(int nr)
{
for( int i=1; i<=nr; i++)
std::cout << (rand() % 6 + 1) <<std::endl;
}
To use std::rand() you need to include the <cstdlib> library. Also, to make the numbers seem random every time you need provide a seed (the time; <ctime>).
#include <iostream>
#include <random>
#include <chrono>
#include <functional>
usingnamespace std;
int main ()
{
mt19937::result_type seed =
chrono::high_resolution_clock::now().time_since_epoch().count();
auto dice = bind(uniform_int_distribution<int>(1, 6), mt19937(seed));
int num;
cout << "How many times do you want to roll the dice? ";
cin >> num;
for (unsignedshortint i = 0; i != num; ++i)
{
std::cout << dice() << '\n';
}
cout << endl;
return 0;
}
Also, for how many 1, 2, 3s etc. you get it would be like this: