473,385 Members | 1,492 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,385 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 13870
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.