473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 13871
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

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

Similar topics

0
by: soss | last post by:
I want to make a program that read a text file and encrypt it by add 1 to each character value , for example: before encryption: HELLO Afater encryption: IFMMP Thank you
0
by: blah123 | last post by:
I took a C++ program a while ago but have totally forgotten how to code it now. I want to make a small program that telnet's into server kronos, enters in loginID (press tab), enters in password,...
1
by: lenest | last post by:
I need help writing a program.... You are to write a python program to accomplish the following: a.. Play a dice game of Craps using a random number generator to simulate the roll of the...
1
by: tracethepath | last post by:
hello everybody, I am a beginner in C++. I got a CD with a C++ how to program,fifth edition book by Deitel which has the source codes included in the books. The source code for ATM case study is in...
3
by: jack 1 | last post by:
how towrite a complete Java application using a one-dimensional array to keep track of the toys in a shop has in its warehouse. It can enter or remove items from the inventory list, change the...
2
by: ilogboy | last post by:
Below is a copy of my Hangman project... I am missing a couple more functions and I don't know how to enhance it... Here are the things I want it to do: 1) I want it to determine the scores for...
2
by: WP | last post by:
Hello, below is my very first python program. I have some questions regarding it and would like comments in general. I won't be able to get my hands on a good python book until tomorrow at the...
32
by: falconsx23 | last post by:
I am making a game called Set, it is a card game: here is a brief description of the rules: The game of Set is played with 81 cards, each of which has 4 properties (number, pattern, color and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.