rand()

I understand everything here, I coded it the only thing im not 100% sure on is
randomNumber = (rand() % 6) + 1 why do people use a modulus to get a number between 1 and 6. does it have something to do with RAND_MAX I tried displaying it its 32700 on my machine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 int randomNumber = 0;
	int choice = 0;
	srand(static_cast<unsigned int>(time(0)));
	while (true)
	{
		std::cout << "\nPress 1 to roll dice and 2 to exit:  ";
		std::cin >> choice;
		if (choice == 2)
		{
			break;
		}
		randomNumber = (rand() % 6) + 1;
		std::cout << "\nYou rolled:  " << randomNumber << "\n";
	}
	std::cout << "\n\nThank-You for rolling with us\n\n";
	system("PAUSE");
	return 0;
randomNumber = (rand() % 6) + 1; Is suppose to look like this -

andomNumber = rand() % 6 + 1.

And to answer your question. It is relevant to RAND_MAX. Read here - http://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator
Yea thanks man I understand it now, also read that its a crappy way of generating random numbers. But it will do for now since I don't know of any other methods yet, reading up on them now.
Its the C style way of doing things. There is a better way for c++. But It'll do for now! its not that important :)
Topic archived. No new replies allowed.