Freeciv-3.3
|
Go to the source code of this file.
Macros | |
#define | log_rand log_debug |
#define | STD_RAND_VALUE_LOG(_c, _s, _nr, _f, _l) |
#define | RAND_VALUE_LOG STD_RAND_VALUE_LOG |
#define | LARGE_PRIME (10007) |
#define | SMALL_PRIME (1009) |
Functions | |
RANDOM_TYPE | fc_rand_debug (RANDOM_TYPE size, const char *called_as, int line, const char *file) |
void | fc_srand (RANDOM_TYPE seed) |
void | fc_rand_uninit (void) |
bool | fc_rand_is_init (void) |
RANDOM_STATE | fc_rand_state (void) |
void | fc_rand_set_state (RANDOM_STATE state) |
void | test_random1 (int n) |
RANDOM_TYPE | fc_randomly_debug (RANDOM_TYPE seed, RANDOM_TYPE size, const char *called_as, int line, const char *file) |
Variables | |
static RANDOM_STATE | rand_state |
#define LARGE_PRIME (10007) |
#define RAND_VALUE_LOG STD_RAND_VALUE_LOG |
#define SMALL_PRIME (1009) |
RANDOM_TYPE fc_rand_debug | ( | RANDOM_TYPE | size, |
const char * | called_as, | ||
int | line, | ||
const char * | file | ||
) |
Returns a new random value from the sequence, in the interval 0 to (size-1) inclusive, and updates global state for next call. This means that if size <= 1 the function will always return 0.
Once we calculate new_rand below uniform (we hope) between 0 and MAX_UINT32 inclusive, need to reduce to required range. Using modulus is bad because generators like this are generally less random for their low-significance bits, so this can give poor results when 'size' is small. Instead want to divide the range 0..MAX_UINT32 into (size) blocks, each with (divisor) values, and for any remainder, repeat the calculation of new_rand. Then: return_val = new_rand / divisor; Will repeat for new_rand > max, where: max = size * divisor - 1 Then max <= MAX_UINT32 implies size * divisor <= (MAX_UINT32 + 1) thus divisor <= (MAX_UINT32 + 1) / size
Need to calculate this divisor. Want divisor as large as possible given above constraint, but it doesn't hurt us too much if it is a bit smaller (just have to repeat more often). Calculation exactly as above is complicated by fact that (MAX_UINT32 + 1) may not be directly representable in type RANDOM_TYPE, so we do instead: divisor = MAX_UINT32 / size
Return whether the current state has been initialized.
Definition at line 200 of file rand.c.
Referenced by init_game_seed(), and sg_save_random().
void fc_rand_set_state | ( | RANDOM_STATE | state | ) |
Replace current rand_state with user-supplied; eg for save/restore. Caller should take care to set state.is_init beforehand if necessary.
Definition at line 229 of file rand.c.
Referenced by map_fractal_generate(), sg_load_random(), sg_load_random(), sg_load_sanitycheck(), sg_load_sanitycheck(), and test_random1().
RANDOM_STATE fc_rand_state | ( | void | ) |
Return a copy of the current rand_state; eg for save/restore.
Definition at line 208 of file rand.c.
Referenced by loaddata_new(), loaddata_new(), map_fractal_generate(), sg_load_random(), sg_load_random(), sg_save_random(), and test_random1().
RANDOM_TYPE fc_randomly_debug | ( | RANDOM_TYPE | seed, |
RANDOM_TYPE | size, | ||
const char * | called_as, | ||
int | line, | ||
const char * | file | ||
) |
void fc_srand | ( | RANDOM_TYPE | seed | ) |
Initialize the generator; see comment at top of file.
Definition at line 161 of file rand.c.
Referenced by client_main(), fcmp_init(), init_game_seed(), and map_fractal_generate().
Test one aspect of randomness, using n numbers. Reports results to LOG_TEST; with good randomness, behaviourchange and behavioursame should be about the same size. Tests current random state; saves and restores state, so can call without interrupting current sequence.
Definition at line 253 of file rand.c.
Referenced by srv_ready().
|
static |
Definition at line 85 of file rand.c.
Referenced by fc_rand_debug(), fc_rand_is_init(), fc_rand_set_state(), fc_rand_state(), fc_rand_uninit(), and fc_srand().