Mail Archives: djgpp/1999/01/08/04:07:23
tim park <sethpark AT bellsouth DOT net> wrote:
>Ok, I know this doesn't have all that much to do with djgpp, but say I
>wanted a random number between 0 and 20. How would I do that? Would you
>please just write me just some code (in c please!!) that would do that?
>Sad air
Here is an example from PROGRAMMING WITH C++ by John Hubbard
(ISBN 0-07-114328-9), a very good text book on C++ to get you going.
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
main()
{
unsigned seed = time(NULL); // assigns the number of seconds
// elapsed in 20th century to seed
srand(seed); // seed the randomiser
for (int i = 0; i < 32; i++)
cout << 1 + rand()%6 << " "; // simulate dice - random number would
// be from 0 to 5 hence the 1 +
cout << endl;
}
Hope it helps
Gerhard
- Raw text -