From c0943b9f7d22b904e8fea48403ad0efbe9ac890f Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Mon, 4 Jul 2011 17:13:29 +1000 Subject: [PATCH] Move (and abstract) the random number functions to utils.c --- src/trader.c | 5 +---- src/utils.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 10 ++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/trader.c b/src/trader.c index bf836df..3541849 100644 --- a/src/trader.c +++ b/src/trader.c @@ -262,11 +262,8 @@ playing that game. If GAME is not specified, start a new game.\n\n\ void init_program (void) { - time_t curtime = time(NULL); // NB: time_t may be larger than long int - - // Initialise the random number generator - srand48((long int) curtime); + init_rand(); // Initialise the terminal display init_screen(); diff --git a/src/utils.c b/src/utils.c index 9e38611..cad6019 100644 --- a/src/utils.c +++ b/src/utils.c @@ -260,3 +260,48 @@ void errno_exit (const char *format, ...) exit(EXIT_FAILURE); } + + +/*----------------------------------------------------------------------- + Function: init_rand - Initialise the random number generator + Arguments: (none) + Returns: (nothing) + + This function initialises the pseudo-random number generator. +*/ + +void init_rand (void) +{ + time_t curtime = time(NULL); // NB: time_t may be larger than long int + srand48((long int) curtime); +} + + +/*----------------------------------------------------------------------- + Function: randf - Return a random number between 0.0 and 1.0 + Arguments: (none) + Returns: double - The random number + + This function returns a pseudo-random number between 0.0 (inclusive) + and 1.0 (not inclusive) as a floating-point number. +*/ + +extern double randf (void) +{ + return drand48(); +} + + +/*----------------------------------------------------------------------- + Function: randi - Return a random number between 0 and limit + Arguments: limit - Upper limit of random number + Returns: int - The random number + + This function returns a pseudo-random number between 0 (inclusive) and + limit (not inclusive) as an integer. +*/ + +extern int randi (int limit) +{ + return randf() * (double) limit; +} diff --git a/src/utils.h b/src/utils.h index 4bfab6c..03b8080 100644 --- a/src/utils.h +++ b/src/utils.h @@ -44,6 +44,8 @@ * Utility function declarations * ************************************************************************/ +// Initialisation and environment functions + extern void init_program_name (char *argv[]); extern const char *program_name (void); @@ -52,10 +54,18 @@ extern const char *data_directory (void); extern char *game_filename (const int game_num); +// Error-reporting functions + extern void err_exit (const char *format, ...) __attribute__((noreturn, format (printf, 1, 2))); extern void errno_exit (const char *format, ...) __attribute__((noreturn, format (printf, 1, 2))); +// Random number functions + +extern void init_rand (void); +extern double randf (void); +extern int randi (int limit); + #endif /* included_UTILS_H */