use random to get a number die

I wrote a program about getting a die number. But this die will give number 1, 2, 4 , 5 , 7, 8. Therefore, i used if control statement to get these numbers. However, I got number 8 only. I don't what problem is. Could you help me to solve the problem?

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
	int die;

	die = (1 + rand() % 6);

	if (die == 3)
	{
		die = 4;
	}
	if (die ==4)
	{
		die = 5;
	}
	if (die == 5)
	{
		die = 7;
	}
	if (die == 6)
	{
		die = 8;
	}

	for (int i = 1; i <= 10; i++)
	{
		cout << die << endl;
	}
	return 0;
}
Last edited on
After posting this question, I may find out the solution. But it produced some number only, not entire numbers in this range are produced. For example, it gave number 1,7,8 randomly. Is it a problem?

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
	int die;

	for (int i = 1; i <= 10; i++)
	{
		die = (1 + rand() % 6);

		if (die == 3)
		{
			die = 4;
		}
		if (die == 4)
		{
			die = 5;
		}
		if (die == 5)
		{
			die = 7;
		}
		if (die == 6)
		{
			die = 8;
		}

		cout << die << endl;
	}
	return 0;
}
Last edited on
You need to use srand to seed the random number generator, otherwise you will always get the same sequence of numbers from rand().

http://www.cplusplus.com/reference/cstdlib/srand/
Topic archived. No new replies allowed.