initdice() is just meant to make the code look nicer. the main call is the line which adds up the result, so the call to rand could be written like:

number = MIN+(int)((double)MAX*rand()/(RAND_MAX+1.0));

RAND_MAX is a predefined, system dependent value, but you needn't really care. MIN and MAX are the respective minimum/maximum values you require. The call also seems to produce a somewhat equal distribution.

Share this post

Link to post

Share on other sites

quote: Original post by Twig Meister If there's a better way please tell me but as a tutor once told me if it works then it cannot be wrong.

Egads! Fire that tutor! It's very easy to come up with examples that work for a particular domain/time/whatever, but fall apart when you least expect it. Debugging and fixing those problems can be quite nasty.

Once knowing that (rand() % X) gives you an integer from zero to X-1 inclusive, try and figure out why this will work. (Hmmm... the get_random_number() function really should check to make sure that 'lo' is less than or equal to 'hi', otherwise it will have problems... I leave that as an exercise.

Finally, in general, srand() should be called ONLY ONCE at the beginning of your program. Typical usage is srand(time(NULL)), which will give you a new pseudo-random sequence every time you run the program.