Dice roll, randomGenerator

The program works fine but how can I get a output like this?(Rounding of the precent, to whole numbers without decimals)

1:s = 1    Precent = 14
2:s = 1    Precent = 14
3:s = 0    Precent = 0
4:s = 0    Precent = 0
5:s = 2    Precent = 29
6:s = 3    Precent = 43


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <random>
#include <ctime>

using namespace std;

int main()
{
    int num;
 
    mt19937 randomGenerator(time(0));
    uniform_int_distribution<int> diceRoll(1, 6);
    
    cout << "How many integers do you want to randomize (<=100):";
    cin >> num;
    
    for( int i=0 ; i<num ; ++i )
        cout << diceRoll(randomGenerator) << endl;
}
@gunnerfunner I'm not quite sure what to look for in the link, because I can't find the answer for my question.
Then I've misunderstood your q
@Gunnerfunner
Maybe, or maybe I'm blind or something. I will try to explain it better if that now is the problem.

I want the same output as this program,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
   int scores[1+6] = { 0 };      // DELIBERATELY over-length: going to ignore the [0] index
   int dots;
   int N;

   srand( time(0) );
   cout << "How many rolls? ";   cin >> N;
   
   for (int r = 0; r < N; r++)
   {
      dots = rand()%6 + 1;       // throw a die
      scores[dots]++;            // increment the tally for this score
   }

   // Output results   
   for ( dots = 1; dots <= 6; dots++ ) cout << "Score " << dots << ":  tally = " << scores[dots] << "     percentage = " << 100.0 * scores[dots] / N << endl;
}


But I want to use the randomGenerator instead of srand() and rand().
Last edited on
You were quite close:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <random>
#include <ctime>

using namespace std;

int main()
{
    int num;
    int scores[1+6] = { 0 };

    mt19937 randomGenerator(time(0));
    uniform_int_distribution<int> diceRoll(1, 6);

    cout << "How many integers do you want to randomize (<=100):";
    cin >> num;

    for( int i=0 ; i<num ; ++i )
        scores[diceRoll(randomGenerator)]++;
    for ( int dots = 1; dots <= 6; dots++ ) cout << "Score " << dots << ":  tally = " << scores[dots] <<
    "     percentage = " << 100.0 * scores[dots] / num << endl;
}
Thanks @gunnerfunner!
One last thing, how do I get the percentage to round of to a whole number, without any decimal at all.
1
2
3
#include <cmath>
 for ( dots = 1; dots <= 6; dots++ ) cout << "Score " << dots << ":  tally = " << scores[dots] <<
    "     percentage = " << round(100.0 * scores[dots] / N) << endl;

Note: percentages may not sum to 100 and so for one of them you'd have to get it as 100 - (sum of the other 5)
closed account (E0p9LyTq)
markusfurst wrote:
But I want to use the randomGenerator instead of srand() and rand()


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <chrono>
#include <random>
#include <array>
#include <cmath>

int main()
{
   // obtain a seed from the system clock
   unsigned seed = (unsigned) (std::chrono::system_clock::now().time_since_epoch().count());

   // mt19937 is a standard mersenne_twister_engine
   std::mt19937 generator(seed);

   // discard some values for better randomization
   generator.discard(generator.state_size);

   // set a distribution range (0 - 5)  [dice roll - 1]
   std::uniform_int_distribution<unsigned> distribution(0, 5);

   // create an array to hold the die rolls
   std::array<unsigned, 6> die_rolls = { 0 };

   std::cout << "How many rolls? ";
   unsigned number_rolls;
   std::cin >> number_rolls;
   std::cout << "\n";

   for (unsigned loop = 0; loop < number_rolls; loop++)
   {
      // roll a die
      unsigned dots = distribution(generator);

      // add the roll to the vector
      die_rolls[dots]++;

      // these two steps could have been combined:
      // die_rolls[distribution(generator)]++;
   }

   // output results   
   for (unsigned loop = 0; loop < die_rolls.size(); loop++)
   {
      std::cout << "Score " << (loop + 1) << ":  tally = " << die_rolls[loop] << "\tpercentage = " << (unsigned) std::round(100.0 * die_rolls[loop] / number_rolls) << "\n";
   }
}

How many rolls? 5000

Score 1:  tally = 860   percentage = 17
Score 2:  tally = 797   percentage = 16
Score 3:  tally = 853   percentage = 17
Score 4:  tally = 855   percentage = 17
Score 5:  tally = 815   percentage = 16
Score 6:  tally = 820   percentage = 16

Why do I use unsigned? The number of die pips and the number of rolls can't be negative.

The displayed percentage totals may not be equal to 100%, a result of rounding.
Last edited on
closed account (E0p9LyTq)
Instead of using std::round(), consider using std::nearbyint():

http://www.cplusplus.com/reference/cmath/nearbyint/

A quick multiple-run test appears to yield percentage totals that are 100% consistently.

Lower number of die rolls will still have rounding errors.
Last edited on
// discard some values for better randomization

not quite, rather for generator.stat_size (line 16) # turns, no random nos are generated, or as cplusplus.com says, it
twist(s) the entire state sequence

http://www.cplusplus.com/reference/random/mersenne_twister_engine/discard/
closed account (E0p9LyTq)
Hmmmmmm..... what exactly did I say? "Discard some values for better randomization."

http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine/discard

Advances the internal state by z times. Equivalent to calling operator() z times and discarding the result


So, the comment is "quite."
Thanks for the additional links, thanks
Topic archived. No new replies allowed.