I wanted to create a simple program that rolls dice randomly and measures what dice face comes up, the frequency, and overall percentage of each face. It's for a six-sided dice. I haven't really done much with random numbers before. So, output would be something like -
// L03Random1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int d1;
int d2;
int d3;
int d4;
int d5;
int d6;
unsigned seed;
int i;
cout << "How many times do you want to throw the dice (0 or less will stop the program)? ";
cin >> seed;
while (seed > 0)
{
srand(seed);
for (i=0; i <= seed; i++)
{
cout << setw(6) << rand();
cout << endl;
}
}
return 0;
}
1. You are seeding with the number of times you want to throw the dice? That means that if you throw the dice 10 times, you'll ALWAYS get the same sequence output. Put srand( time(NULL) ); at the start of int _tmain() and delete it at line 27.
2. line 30 will display rand, but then the number is lost. Next time you call rand(), it'll be a different number. Always store your random number so that you can use it later.
3. You aren't counting the frequency of each face.
You can satisfy 2 & 3 with this (assuming you initialize d1 thru d6 to 0)
1 2 3 4 5 6 7 8 9 10 11 12 13
for (i = 0; i <= seed; i++)
{
int generatedNum = rand();
switch (generatedNum)
{
case 1: d1++; break;
case 2: d2++; break;
case 3: d3++; break;
case 4: d4++; break;
case 5: d5++; break;
case 6: d6++; break;
}
}
Your random number will be something between 0 and RANDMAX. Try this to get a number between 1 and 6: rand()%spread+min; where spread is 6 and min is 1... so rand()%6+1;
// L03Random1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
srand( time(NULL) );
int d1 = 0;
int d2 = 0;
int d3 = 0;
int d4 = 0;
int d5 = 0;
int d6 = 0;
unsigned seed;
int i;
cout << "How many times do you want to throw the dice (0 or less will stop the program)? ";
cin >> seed;
if (seed > 0)
{
for (i = 0; i <= seed; i++)
{
int generatedNum = rand();
switch (generatedNum)
{
case 1: d1++; break;
case 2: d2++; break;
case 3: d3++; break;
case 4: d4++; break;
case 5: d5++; break;
case 6: d6++; break;
}
}
cout << "Face Frequency Percentage" << endl;
cout << "d1 " << d1 <<" " << d1 / seed << endl;
}
elsereturn 0;
}
#include <iostream>
#include <random>
#include <ctime>
usingnamespace std;
int main()
{
// array of elements representing the different faces on a die.
// element 0 corresponds to a die roll of 1, 1 to 2, 3 to 4, 4 to 5 and 5 to 6.
int die_face[6] = {} ; // all elements initialized to 0.
srand(time(NULL)) ;
unsigned iterations = 0 ;
cout << "How many times do you want to throw the dice (0 or less will stop the program)? ";
cin >> iterations ;
for ( unsigned i=0; i<iterations; ++i )
++die_face[rand()%6] ;
}
I think I got it now. However, I can't seem to get the results to line up. Like if frequency is double digits for some and single digits for others, then percentage alignment is mixed up. The cursor starts farther to the right for some.
Try something like this. Instead of using a ton of consecutive spaces, you can use a tab by putting "\t" in there. That will align itself every so often. If you still have problems, then you'll need to look into the <iomanip> header at the setw function. http://cplusplus.com/reference/iostream/manipulators/setw/
To answer your question earlier, the breakstatement gets you out of the switch control.