Freeciv-3.2
Loading...
Searching...
No Matches
Macros | Functions | Variables
rand.c File Reference
#include "log.h"
#include "shared.h"
#include "support.h"
#include "rand.h"

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
 

Macro Definition Documentation

◆ LARGE_PRIME

#define LARGE_PRIME   (10007)

◆ log_rand

#define log_rand   log_debug

Definition at line 45 of file rand.c.

◆ RAND_VALUE_LOG

#define RAND_VALUE_LOG   STD_RAND_VALUE_LOG

Definition at line 77 of file rand.c.

◆ SMALL_PRIME

#define SMALL_PRIME   (1009)

◆ STD_RAND_VALUE_LOG

#define STD_RAND_VALUE_LOG (   _c,
  _s,
  _nr,
  _f,
  _l 
)
Value:
log_rand("%s(%lu) = %lu at %s:%d", _c, \
(unsigned long) _s, (unsigned long) _nr, \
_f, _l);
char * incite_cost
Definition comments.c:75
#define log_rand
Definition rand.c:45

Definition at line 48 of file rand.c.

Function Documentation

◆ fc_rand_debug()

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

Definition at line 114 of file rand.c.

◆ fc_rand_is_init()

bool fc_rand_is_init ( void  )

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().

◆ fc_rand_set_state()

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().

◆ fc_rand_state()

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().

◆ fc_rand_uninit()

void fc_rand_uninit ( void  )

Mark fc_rand state uninitialized.

Definition at line 192 of file rand.c.

Referenced by srv_main().

◆ fc_randomly_debug()

RANDOM_TYPE fc_randomly_debug ( RANDOM_TYPE  seed,
RANDOM_TYPE  size,
const char called_as,
int  line,
const char file 
)

Local pseudo-random function for repeatedly reaching the same result, instead of fc_rand(). Primarily needed for tiles.

Use an invariant equation for seed. Result is 0 to (size - 1).

Definition at line 292 of file rand.c.

◆ fc_srand()

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_random1()

void test_random1 ( int  n)

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().

Variable Documentation

◆ rand_state

RANDOM_STATE rand_state
static