11 Replies - 1542 Views - Last Post: 21 February 2015 - 10:04 PM Rate Topic: -----

#1 Hexd   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 11

Expected outcome/error on X amount of rolls

Posted 20 February 2015 - 09:12 PM

I have a program that rolls two dice however many times the user specifies and counts the occurrences of each total (2-12). I have to compare the results of the random rolls to the expected outcome and give the percentage of error between the two.

I have an example that tells me the sum to expected outcome are as follows if I roll 36 times: 2/1, 3/2, 4/3, 5/4, 6/5, 7/6, 8/5, 9/4, 10/3, 11/2, 12/1 but I don't know how to put that into code to get the expected outcome for X amount of rolls. That is what i'm asking for help with.

Is This A Good Question/Topic? 0
  • +

Replies To: Expected outcome/error on X amount of rolls

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Expected outcome/error on X amount of rolls

Posted 20 February 2015 - 09:22 PM

You're going to need to show your code before anyone will be able to help.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Hexd   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 11

Re: Expected outcome/error on X amount of rolls

Posted 20 February 2015 - 09:30 PM

#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>

using namespace std;

int RandomRoll(){
	int random = (rand() % 6) + 1;
	return random;
}

void main(){
	// seed
	srand((unsigned int)time(NULL));

	int totals[12] = { 0 }; 
	int totalRolls, runAgain;

	do {
		// Gather input
		cout << "This program will roll two dice and tabulate the results." << endl;
		cout << "Enter number of total rolls: ";
		cin >> totalRolls;

		// Fill array with data according to total rolls
		for (int i = 0; i < totalRolls; i++) { 
			int result = RandomRoll() + RandomRoll() -1;
			totals[result]++;
		
		}

		// Output table
		cout << endl << "Sum #\t\t" << "Result\t\t" << "Odds\t\t" << "% of Error\t\t" << endl;
		for (int i = 0; i < 11; i++){
			cout << i + 1 << ": \t\t" << totals[i] << "\t\t"  << endl;
																			
		}

		cout << "Enter 1 to run again: " << endl;
		cin >> runAgain;
	} while (runAgain == 1);

	system("pause");

}
 

Was This Post Helpful? 0
  • +
  • -

#4 Hexd   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 11

Re: Expected outcome/error on X amount of rolls

Posted 20 February 2015 - 11:08 PM

View PostHexd, on 20 February 2015 - 09:30 PM, said:

#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>

using namespace std;

int RandomRoll(){
	int random = (rand() % 6) + 1;
	return random;
}

void main(){
	// seed
	srand((unsigned int)time(NULL));

	int totals[12] = { 0 }; 
	int totalRolls, runAgain;

	do {
		// Gather input
		cout << "This program will roll two dice and tabulate the results." << endl;
		cout << "Enter number of total rolls: ";
		cin >> totalRolls;

		// Fill array with data according to total rolls
		for (int i = 0; i < totalRolls; i++) { 
			int result = RandomRoll() + RandomRoll() -1;
			totals[result]++;
		
		}

		// Output table
		cout << endl << "Sum #\t\t" << "Result\t\t" << "Odds\t\t" << "% of Error\t\t" << endl;
		for (int i = 0; i < 11; i++){
			cout << i + 1 << ": \t\t" << totals[i] << "\t\t"  << endl;
																			
		}

		cout << "Enter 1 to run again: " << endl;
		cin >> runAgain;
	} while (runAgain == 1);

	system("pause");

}
 


I should add that in my output table function I had added some test code while trying to work it out in my head, as well as an array with the expected outcome for 36 which I may end up putting back because I suspect that will be useful, and it was only the way I was trying to use it that was useless. I don't want the code to reflect that I didn't even attempt to fix/research this first. I could not find a way to manipulate the expected outcome (1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1) for 36 rolls to (10, 20, 30, etc) if the rolls were 360 in a way that would adapter to ANY roll.
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Expected outcome/error on X amount of rolls

Posted 21 February 2015 - 12:37 AM

02 #include <cstdlib>
03 #include <stdlib.h>
04 #include <time.h>
cstdlib and stdlib.h are the same file, so you only need the former.
Replace time.h with ctime.

> void main()
main returns an int.
$ g++ -Wall foo.cpp
foo.cpp:13:11: error: ‘::main’ must return ‘int’



> for (int i = 0; i < 11; i++)
How many times do you think two die rolls added together will equal 0?


> I could not find a way to manipulate the expected outcome (1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1) for 36 rolls
I don't think you understand randomness if this is what you're expecting.
Whilst this is what you expect over a very long series of experiments, expecting that exact output every time you type in 36 just isn't going to happen.


> int random = (rand() % 6) + 1;
http://c-faq.com/lib/notveryrand.html
Was This Post Helpful? 2
  • +
  • -

#6 Hexd   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 11

Re: Expected outcome/error on X amount of rolls

Posted 21 February 2015 - 01:18 AM

View PostSalem_c, on 21 February 2015 - 12:37 AM, said:

> I could not find a way to manipulate the expected outcome (1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1) for 36 rolls
I don't think you understand randomness if this is what you're expecting.
Whilst this is what you expect over a very long series of experiments, expecting that exact output every time you type in 36 just isn't going to happen.


Thank you for the help. I know that it won't be a very accurate prediction. It's there only to compare when using a large amount of rolls. I need something to compare it to and generate the % of error.
Was This Post Helpful? 0
  • +
  • -

#7 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Expected outcome/error on X amount of rolls

Posted 21 February 2015 - 11:28 AM

Hi, the probability of throwing one face/value/number with one six-side die (1D6) is 1/6. The probability of throwing a sum of two dice is different, because some totals can be achieved by using different face combinations.

The total number of possible outcomes/combinations is 6 x 6 = 36.

The lowest sum that can occur is two. That only happens when each die is 1, so the probability is 1/36.

With the total of three, there are two possible combinations. Die A is 2 and B is 1, and also die A is 1 and B is 2. Giving a possibility of 2/36.

So you could have a list of the number of combinations for each total, and calculate the probability.

What is a Probability Distribution

Dice - UW Faculty Wiki
Was This Post Helpful? 0
  • +
  • -

#8 Hexd   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 11

Re: Expected outcome/error on X amount of rolls

Posted 21 February 2015 - 05:24 PM

View Post#define, on 21 February 2015 - 11:28 AM, said:

Hi, the probability of throwing one face/value/number with one six-side die (1D6) is 1/6. The probability of throwing a sum of two dice is different, because some totals can be achieved by using different face combinations.

The total number of possible outcomes/combinations is 6 x 6 = 36.

The lowest sum that can occur is two. That only happens when each die is 1, so the probability is 1/36.

With the total of three, there are two possible combinations. Die A is 2 and B is 1, and also die A is 1 and B is 2. Giving a possibility of 2/36.

So you could have a list of the number of combinations for each total, and calculate the probability.

What is a Probability Distribution

Dice - UW Faculty Wiki


If I plug 36 into my program I need the answer to be 6 for the sum of 7. I need it to be 60 for the sum of 7 with 360 rolls. I need it to be 6000 for the sum of 7 if I roll 36000 times. Does this make sense? This is and has been my inquiry from the start. I don't know what equation would calculate this.
Was This Post Helpful? 0
  • +
  • -

#9 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Expected outcome/error on X amount of rolls

Posted 21 February 2015 - 07:25 PM

Hi, you can calculate as a fraction of the whole. A problem could be not accounting for integer division, so cast to a double for the calculation from integers.

numberOfRolls = 36;
numberOfSum7  =  6;

// fraction of one
double fraction = (double) numberOfSum7 / numberOfRolls;

// percentage of total number of rolls
double percent = fraction * 100;




You can calculate the expected as well as the test that way. It could be a little function.
Was This Post Helpful? 1
  • +
  • -

#10 Hexd   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 11

Re: Expected outcome/error on X amount of rolls

Posted 21 February 2015 - 08:42 PM

I think perhaps my question is unclear, replies only seem to mention it in passing. On a test run I rolled 50 times. 7 was rolled 11 of those times, no problems there. The next part of the table is where I don't know what to do. I am being asked to output the expected number of occurrences in this portion for each sum based on how many rolls there are.

cout << endl << "Sum #\t\t" << "Result\t\t" << "Odds\t\t" << "% of Error\t\t" << endl;
for (int i = 1; i < 12; i++){
cout << i + 1 << ": \t\t" << totals[i] << "\t\t"<< *expected outcome goes here* << endl; }



I am outputting the table of possible sums, then I output what my random function rolls. That all is fine, the odds column is my immediate problem. What do I put in "expected outcome goes here" to get the expected outcome? I cannot fathom what to type in there.
Was This Post Helpful? 0
  • +
  • -

#11 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Expected outcome/error on X amount of rolls

Posted 21 February 2015 - 09:27 PM

You will need another array to store some data for the expected outcome for each value.

For 7 the expected outcomes is 6 per 36 rolls. 6 / 36 is the fraction of outcomes expected, so (6 * 50) / 36 is the expected outcomes for 50 which equals 8.33
Was This Post Helpful? 1
  • +
  • -

#12 Hexd   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 11

Re: Expected outcome/error on X amount of rolls

Posted 21 February 2015 - 10:04 PM

View Post#define, on 21 February 2015 - 09:27 PM, said:

You will need another array to store some data for the expected outcome for each value.

For 7 the expected outcomes is 6 per 36 rolls. 6 / 36 is the fraction of outcomes expected, so (6 * 50) / 36 is the expected outcomes for 50 which equals 8.33


Thank you. I had the array but not the knowledge of exactly how to use it. It is now functioning as intended.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1