Function of Dice simulation

Hello there folks. I am here to request help. Please don't misunderstand me. I don't want anyone to write me some code for free while I just sit here being lazy. My situation is that I have a terrible professor and until now I was able to search around and teach myself how to do most of the assignments. But now things seem to be escalating and I am no longer finding the information I need to understand what I need to easily.

Here is what I need: A function that will simulate a roll of a die, returning values from 1 to 6 outside of the main program. The main program then will use a user defined number for the random number seed and it will calculate 1000000 rolls. Then showing the results for each side of the die. I will also need a histogram but like I said I don't want a freebie. My request really is that maybe some of you guys have seen another post here, or some easy to understand guide somewhere that I haven't' been able to find. I just need a push in the right direction. There is a lot more to the program. But once I am headed the right direction I think I will be able to manage.

Thanks in advance.
Check out the Reference in the C library <cstdlib> for srand() and rand() for examples of how to get a random number in 1..6. Put the initialization at the beginning of main() and put the call to rand() % 6 + 1 in a function.

Any time you do something over and over you should use a loop.

A histogram is just an array of counts corresponding to values. Your histogram might be defined as:

1
2
3
unsigned histogram[ 7 ] = { 0 };
// We'll ignore element 0. Elements 1..6 represent each side of the die. 
// Initially, no side of the die has been rolled. 

Each time you roll the die (by calling your function), update the corresponding index in the histogram by adding 1 to it.

When you print your array, you should find that the numbers in the histogram should be almost equal for each side of the die. (You'll find that the first couple of elements will be slightly larger, but you may not notice this. Unless your professor gives you grief over it, don't worry about it. The fix is a good ten lines of code.) That is, they should all equal about 1000000/6, or 166667.

Hope this helps.
Thank you very much my good sir. I have since accomplished quite a bit but I am still having a hard time with the histogram... so far this is what I got
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdio>
#include <cstdlib>
#include <ctime>
#define N 1000000

using namespace std;

double rand_double();
int Random(int Max);
void dicewtfone();
void dicewtftwo(int argc, char* argv[]);

int main(int argc, char* argv[])
{
	dicewtfone();
	dicewtftwo(argc, argv);
}

void dicewtfone()
{
	int d[7];
	int i, k, roll, num1, num2, num3;
	srand((28420));
	for (i = 1; i < 7; i++) d[i] = 0;
	for (k = 0; k < N; k++)
	{
		roll = (int)(6.0*rand_double() + 1.0);
		d[roll]++;
	}
	for (i = 1; i < 7; i++)
		printf("%2i:   %6i :\n", i, d[i]);
}

double rand_double()
{
	return rand() / (double)RAND_MAX;
}

void dicewtftwo(int argc, char* argv[])
{
	int i, num1, num2, num3;
	int totals[19];
	srand((28420));

	for (i = 3; i <= 18; i++)
		totals[i] = 0;

	for (i = 0; i < N; i++)
	{
		int d1 = Random(6);
		int d2 = Random(6);
		int d3 = Random(6);
		int total = d1 + d2 + d3;
		totals[total]++;
	}
	for (i = 3; i <= 18; i++)
		printf("%2i:   %6i : \n", i, totals[i]);
}

int Random(int Max)
{
	return (rand() % Max) + 1;
}


It is doing everything the professor is asking with the exception that the histogram is suppose to be outputted after each number giving one * for every 5 thousand times.

I tried adding something like this using variables used on the current code but it didn't work at all...
1
2
3
4
5
6
7
8
9
10
11
void histogram()
{
	int i, j;

	for (i = 5000; i <= 200000 ; i +=5000)
	{
		for (j = 5000; j <= i; j += 5000)
			cout << "*";
		cout << endl;
	}
}


Any helpful hints or a good push in the right direction...
If d[2]=12893, then 12893/5000 = 2. Print two *s.

Hope this helps.
Topic archived. No new replies allowed.