JR> generate numbers between a lower bound value and an upper
JR> bound value - in the case of the dice example 1 - 6.
jr>....
Not exactly a C++ question, really, but I suppose one could
construct a dice class easily enough. :)
Test it out and see how it works for you.
//_|_|_| DIECLASS.CPP //
//_|_|_| A simple dice class for C++ programs. //
//_|_|_| No warrantee or guarantee is given or implied. //
//_|_|_| PUBLIC DOMAIN by Kurt Kuzba. (5/18/1997) //
#include
#include
#include
// begin: Dice class definition and function code //
class Dice {
public:
Dice(int dice, int sides)
{
srand((unsigned)time(NULL));
number_dice = dice;
number_sides = sides;
dice_values = new int[dice];
}
~Dice(void) { delete []dice_values; }
int Roll(void)
{
int die, total = 0;
for(die = 0; die < number_dice; die++)
total += (*(dice_values + die) = rnd(number_sides) + 1);
return total;
}
const int *Get_dice(void) { return (const int*)dice_values; }
private:
int rnd(int max)
{
return (int)((((long)rand() * (long)max) - 1L)
/ (long)RAND_MAX);
}
int *dice_values, number_dice, number_sides;
};
// end: Dice class definition and function code //
// begin: Test program for Dice class //
int main(void)
{
Dice four_by_nine(4, 9);
const int *dice;
int rolls;
cout << "\n\nOur four dice each have nine sides.\n";
for(rolls = 0; rolls < 5; rolls++)
{
cout << "A roll of the dice gives us "
<< four_by_nine.Roll();
dice = four_by_nine.Get_dice();
cout << "\nwith the four dice values being:\n"
<< *(dice + 0) << ", "
<< *(dice + 1) << ", "
<< *(dice + 2) << ", "
<< *(dice + 3) << '\n';
}
cout << "This has been almost nearly random. :)\n" << flush;
return 0;
}
// end: Test program for Dice class functions //
//_|_|_| End DIECLASS.CPP //
> ] People with glass heads shouldn't.. Ummm.. I forgot.........
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)
|