Command that acts like a dice

Hello. I recently tried to create a mini program. I wanted to use a command that works something like a dice.
If it rolled on ONE, it would activate the command I had for ONE.
If it rolled on TWO, it would activate the command I had for TWO.
etc, etc...

Does anyone know how to do that?
Thanks
What you need would be the

rand() function

and the

switch-statement.

Do some search on this board and/or the web and you will find everything you need.

rand() is for rolling the dice and the switch-statement is for executing the corresponding command(s)

int main
Oh. That makes it clearer. I guess it would work.
How exactly does rand() work? Is it a random number between 1-100?
rand itself puts out a number between 1 and RAND_MAX, which is a constant defined in cstdlib. The RAND_MAX is much bigger than 100, but you get the output you want by using the modulo (%6 in your case). This will cause the number rand creates to be divided by 6 and the remainder is what is stored. Thus the number will be between 0-5. Since you want 1-6 all you have to do is put int random_number = rand() % 6 + 1, which will create a random number, take the remainder between 0-5, and add 1.

edit: oops, hamsterman is of course correct. What I said in the first place about %6 being what you want is the correct solution. I'll fix that.
Last edited on
rand()%5 will generate a random number between 0-4 not 0-5
Oh! So that's what the %x is for! Thanks!! =)))
Yep, it's a cool little thing. And Just to clarify, it's not specific to the rand() function, you can use it with any number or function that returns a number.
I write a code to use in a rpg game about dices:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main ()
{
int a;
int b;
int c;

cout<< "   - RPG Dice - \n\n"
<< "Which Dice? ";
cin>> b;
cout << "How many times? ";
cin>> a;
cout<< "\n\n   ";
while (a>1) {
      c = rand() % b + 1;
      cout << c <<", ";
      --a;
      }
cout<< c <<".\n\n";
system ("pause");
return 0;
}      


But there is a problem. Whenever i enter the same values, the program also gives the same values to me. For example;

- RPG Dice -

Which Dice? 8
How many times? 5


2, 4, 7, 5, 5.

Press anykey to continue. . .

When i close and run the program again:

- RPG Dice -

Which Dice? 8
How many times? 5


2, 4, 7, 5, 5.

Press anykey to continue. . .
Last edited on
There's a corresponding function called srand which "seeds" the random algorithm.

A very common way of doing this is by using the execution time as the seed value, thus ensuring that each one is seeded differently.

You just have to include the time library and call srand like this:

1
2
#include <time.h>
srand ( time(NULL) );


Do that before you call rand() and you should be fine.
Yeah, but make sure to call srand() just once, not every time you call rand().
I've never actually called it more than once, what happens?
Well, I'm not sure about rand(), but what happened in QuickBASIC was that (aside from the obvious performance impact) the numbers lost randomness. When I plotted them to the screen they seemed to prefer certain lines. This most likely happens because the random value now depends on the value the system clock happens to have at the time of the call, instead of depending on the random value that came before it.

By the way, I hope we all know by now that these are pseudo-random numbers (unless rand() has been specially crafted to use a system-specific device, such a broken radio, to get its random values).
Last edited on
Yeah, but just about everything (well, that isn't really huge) is pseudo-random.
Topic archived. No new replies allowed.