1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-12-04 14:46:45 -05:00

Move (and abstract) the random number functions to utils.c

This commit is contained in:
John Zaitseff 2011-07-04 17:13:29 +10:00
parent 9a39c84bf4
commit c0943b9f7d
3 changed files with 56 additions and 4 deletions

View File

@ -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();

View File

@ -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;
}

View File

@ -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 */