Roll a dice

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?

1
2
If(rand=1)
   cout << "You rolled" << numbersof1 << "ones";

Something like that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace 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;
}
Last edited on
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>).

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void dice(int nr);

int main ()
{
    srand(time(NULL));

    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;
}


But since you're using C++ you should actually use the C++ ways over the C ways.

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
#include <iostream>
#include <random>
#include <chrono>
#include <functional>
using namespace 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 (unsigned short int 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:

1
2
3
unsigned short int count[6] = { };

++count[random_number - 1];

And to get how many 3s were rolled for example, the count would be in count[3 - 1]. n being the roll to get access to the count would be count[n - 1].
Last edited on
That didn't solve the question I had.
Read my edit above ^^^. The last part.
Thanks, but I don't understand the code that you wrote. Is there someway to do what I requested with the code that I wrote as a base?
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void dice(int nr, int (&arr)[6]);

int main ()
{
    srand(time(NULL));

    int num = 0;
    int count[6] = { };
    
    cout << "How many times do you want to roll the dice? ";
    cin >> num;

    cout << '\n';
    dice(num, count);
    cout << '\n';

    for (unsigned short int n = 1; n != 7; ++n)
    {
        cout << n << "s: " << count[n - 1] << '\n';
    }

    cout << endl;

    return 0;
}

void dice(int nr, int (&arr)[6])
{
    int temp = 0;

    for( unsigned short int i = 0; i != nr; ++i)
    {
        temp = rand() % 6 + 1;
        cout << temp << '\n';
        ++arr[temp - 1];
    }
}
Last edited on
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;
}
Well, I was trying to keep the OP's base design. Concise code you have though.
Thank you both!
Well, I was trying to keep the OP's base design.


Apologies! Couldn't resist!
Topic archived. No new replies allowed.