mirror of
https://github.com/xcat2/xNBA.git
synced 2024-12-14 07:11:32 +00:00
Split random number generation out into core/random.c, and create the
correct prototypes for srandom(), rand() and srand().
This commit is contained in:
parent
f4c1e564a1
commit
4256b3338a
@ -55,24 +55,6 @@ uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new)
|
||||
return (~checksum) & 0xFFFF;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
|
||||
**************************************************************************/
|
||||
long int random(void)
|
||||
{
|
||||
static int32_t seed = 0;
|
||||
int32_t q;
|
||||
if (!seed) /* Initialize linear congruential generator */
|
||||
seed = currticks();
|
||||
/* simplified version of the LCG given in Bruce Schneier's
|
||||
"Applied Cryptography" */
|
||||
q = seed/53668;
|
||||
if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
|
||||
return seed;
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
SLEEP
|
||||
**************************************************************************/
|
||||
|
38
src/core/random.c
Normal file
38
src/core/random.c
Normal file
@ -0,0 +1,38 @@
|
||||
/** @file
|
||||
*
|
||||
* Random number generation
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static int32_t rnd_seed = 0;
|
||||
|
||||
/**
|
||||
* Seed the pseudo-random number generator
|
||||
*
|
||||
* @v seed Seed value
|
||||
*/
|
||||
void srandom ( unsigned int seed ) {
|
||||
rnd_seed = seed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
|
||||
*
|
||||
* @ret rand Pseudo-random number
|
||||
*/
|
||||
long int random ( void ) {
|
||||
int32_t q;
|
||||
|
||||
if ( ! rnd_seed ) /* Initialize linear congruential generator */
|
||||
srandom ( currticks() );
|
||||
|
||||
/* simplified version of the LCG given in Bruce Schneier's
|
||||
"Applied Cryptography" */
|
||||
q = ( rnd_seed / 53668 );
|
||||
rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q );
|
||||
if ( rnd_seed < 0 )
|
||||
rnd_seed += 2147483563L;
|
||||
return rnd_seed;
|
||||
}
|
@ -2,14 +2,27 @@
|
||||
#define STDLIB_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* Numeric parsing
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
|
||||
extern unsigned long strtoul ( const char *p, char **endp, int base );
|
||||
extern void * realloc ( void *old_ptr, size_t new_size );
|
||||
extern void * malloc ( size_t size );
|
||||
extern void free ( void *ptr );
|
||||
extern int system ( const char *command );
|
||||
extern long int random ( void );
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* Memory allocation
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
|
||||
extern void * malloc ( size_t size );
|
||||
extern void * realloc ( void *old_ptr, size_t new_size );
|
||||
extern void free ( void *ptr );
|
||||
extern void * _calloc ( size_t len );
|
||||
|
||||
/**
|
||||
@ -29,4 +42,31 @@ static inline void * calloc ( size_t nmemb, size_t size ) {
|
||||
return _calloc ( nmemb * size );
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* Random number generation
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
|
||||
extern long int random ( void );
|
||||
extern void srandom ( unsigned int seed );
|
||||
|
||||
static inline int rand ( void ) {
|
||||
return random();
|
||||
}
|
||||
|
||||
static inline void srand ( unsigned int seed ) {
|
||||
srandom ( seed );
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* Miscellaneous
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
|
||||
extern int system ( const char *command );
|
||||
|
||||
#endif /* STDLIB_H */
|
||||
|
Loading…
Reference in New Issue
Block a user