TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JIM ROBIN
from: CRAIG MCGREGOR
date: 1997-05-06 17:48:00
subject: Throw of dice

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)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.