mirror of
https://git.zap.org.au/git/trader.git
synced 2025-01-03 14:57:41 -05:00
Move (and abstract) the random number functions to utils.c
This commit is contained in:
parent
9a39c84bf4
commit
c0943b9f7d
@ -262,11 +262,8 @@ playing that game. If GAME is not specified, start a new game.\n\n\
|
|||||||
|
|
||||||
void init_program (void)
|
void init_program (void)
|
||||||
{
|
{
|
||||||
time_t curtime = time(NULL); // NB: time_t may be larger than long int
|
|
||||||
|
|
||||||
|
|
||||||
// Initialise the random number generator
|
// Initialise the random number generator
|
||||||
srand48((long int) curtime);
|
init_rand();
|
||||||
|
|
||||||
// Initialise the terminal display
|
// Initialise the terminal display
|
||||||
init_screen();
|
init_screen();
|
||||||
|
45
src/utils.c
45
src/utils.c
@ -260,3 +260,48 @@ void errno_exit (const char *format, ...)
|
|||||||
|
|
||||||
exit(EXIT_FAILURE);
|
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;
|
||||||
|
}
|
||||||
|
10
src/utils.h
10
src/utils.h
@ -44,6 +44,8 @@
|
|||||||
* Utility function declarations *
|
* Utility function declarations *
|
||||||
************************************************************************/
|
************************************************************************/
|
||||||
|
|
||||||
|
// Initialisation and environment functions
|
||||||
|
|
||||||
extern void init_program_name (char *argv[]);
|
extern void init_program_name (char *argv[]);
|
||||||
extern const char *program_name (void);
|
extern const char *program_name (void);
|
||||||
|
|
||||||
@ -52,10 +54,18 @@ extern const char *data_directory (void);
|
|||||||
|
|
||||||
extern char *game_filename (const int game_num);
|
extern char *game_filename (const int game_num);
|
||||||
|
|
||||||
|
// Error-reporting functions
|
||||||
|
|
||||||
extern void err_exit (const char *format, ...)
|
extern void err_exit (const char *format, ...)
|
||||||
__attribute__((noreturn, format (printf, 1, 2)));
|
__attribute__((noreturn, format (printf, 1, 2)));
|
||||||
extern void errno_exit (const char *format, ...)
|
extern void errno_exit (const char *format, ...)
|
||||||
__attribute__((noreturn, format (printf, 1, 2)));
|
__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 */
|
#endif /* included_UTILS_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user