Hello Jim!
30 Apr 97 07:14, Jim Robin wrote to All:
JR> Hi All,
JR> I've got this friend who is constructing a dice game using Visual
JR> C++ where one of the required elements is to simulate the
JR> throwing of a dice.
JR> To cut a long story short does anyone know of an algorithm that
JR> can generate numbers between a lower bound value and an upper
JR> bound value - in the case of the dice example 1 - 6.
The function rand() should produce a "fairly" random number, the problem
fter
that then is just the number range it uses being much larger than what you
want. This is easily solved however by doing a mod on the number, for
instance something like:
int random(int max) {
return (rand() % max) + 1; // Return a number between 1 and max.
}
main() {
....
dice_score = random(6);
....
}
Regards,
Craig email: cmcgregor@clear.net.nz
Netmail: "Craig McGregor" 3:772/1175@fidonet.org
"Craig McGregor" 8:9000/110@familynet
--- GoldED/2 2.42.G0615
---------------
* Origin: The Lamp (Christian) BBS, Auckland, New Zealand (3:772/1175)
|