472,119 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Dice program..need help

I need help on getting started with a dice program that will output as many random numbers from 1 to 6 and as many rolls as the user requests....And then how many times each number shows up from the number of rolls...
Oct 18 '06 #1
6 13702
Banfa
9,065 Expert Mod 8TB
Create a function that produces a number from 1 to 6.

Then implement this pseudo code

Expand|Select|Wrap|Line Numbers
  1. GET number of require dice rolls
  2.  
  3. FOR each required dice roll
  4.     Get Dice Roll
  5.  
  6.     Increment Count of number rolled
  7. END FOR
  8.  
  9. PRINT Dice roll statistics
  10.  
You can easily store the count of each number rolled in an array of 6 integers.
Oct 18 '06 #2
I need help on getting started with a dice program that will output as many random numbers from 1 to 6 and as many rolls as the user requests....And then how many times each number shows up from the number of rolls...

Try this out, threw it together real quick here because I've got to get back to work, but I compiled it and it should run.

You can clean up the code and rename variables, etc... for your own purposes:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. int rollDice();
  8.  
  9. int main(int argc, char* argv)
  10. {
  11.     int numRolls;
  12.     int arrRolls[6];
  13.     int dieRoll;
  14.  
  15.     srand((unsigned)time(0));
  16.  
  17.     cout << "How many times would you like to roll the dice?" << endl;
  18.     cin >> numRolls;
  19.  
  20.     for (int k = 0; k < 6; k++)
  21.     {
  22.         arrRolls[k] = 0;
  23.     }
  24.  
  25.     for (int i = 1; i <= numRolls; i++)
  26.     {
  27.         dieRoll = rollDice();
  28.         cout << "Die Roll " << i << ": " << dieRoll << endl;
  29.         arrRolls[dieRoll - 1]++;
  30.     }
  31.  
  32.     for (int j = 0; j < 6; j++)
  33.     {
  34.         cout << "The number " << j + 1 << " was rolled " << arrRolls[j] << " times." << endl;
  35.     }
  36. }
  37.  
  38. int rollDice()
  39. {
  40.     int result;
  41.  
  42.     result = (rand() % 6) + 1;
  43.  
  44.     return result;
  45. }
  46.  
Oct 18 '06 #3
Banfa
9,065 Expert Mod 8TB
result = (rand() % 6) + 1;
This is not a good way to get a random value between 1 and 6 because the normal algorithm implemented by rand() does not give very good randomness in the low order bits of the value produced.
Oct 18 '06 #4
This is not a good way to get a random value between 1 and 6 because the normal algorithm implemented by rand() does not give very good randomness in the low order bits of the value produced.
What's a better way?

With 1000 iterations this one seems fairly random, to me:

1 - 168
2 - 150
3 - 170
4 - 173
5 - 168
6 - 171
Oct 18 '06 #5
Banfa
9,065 Expert Mod 8TB
This is a better way

(int)((double)rand() / ((double)RAND_MAX + 1) * N)

Get values in the range 0 - (N-1)

If you want 1-6 then N=6 and add 1 to the result.

If you don't want to use floating point arrithmatic you can use

rand() / (RAND_MAX / N + 1)

However if N is very close to RAND_MAX both these methods break down and you require something else.

It's all explained here.
Oct 18 '06 #6
This is a better way

(int)((double)rand() / ((double)RAND_MAX + 1) * N)

Get values in the range 0 - (N-1)

If you want 1-6 then N=6 and add 1 to the result.

If you don't want to use floating point arrithmatic you can use

rand() / (RAND_MAX / N + 1)

However if N is very close to RAND_MAX both these methods break down and you require something else.

It's all explained here.
Thanks for the reference, Banfa - hadn't looked at that link before, just going off of the knowledge that I already have. This makes sense, though - appreciate the feedback.
Oct 19 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by blah123 | last post: by
3 posts views Thread by jack 1 | last post: by
2 posts views Thread by ilogboy | last post: by
2 posts views Thread by WP | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.