Freeciv-3.3
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions
rand.h File Reference
#include <stdint.h>
#include "log.h"
#include "support.h"

Go to the source code of this file.

Data Structures

struct  RANDOM_STATE
 

Macros

#define RANDLOG_ON
 
#define RANDLOG_OFF
 
#define fc_rand(_size)    fc_rand_debug((_size), "fc_rand", __FC_LINE__, __FILE__)
 
#define fc_randomly(_seed, _size)    fc_randomly_debug((_seed), (_size), "fc_randomly", __FC_LINE__, __FILE__)
 

Typedefs

typedef uint32_t RANDOM_TYPE
 

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)
 

Macro Definition Documentation

◆ fc_rand

#define fc_rand (   _size)     fc_rand_debug((_size), "fc_rand", __FC_LINE__, __FILE__)

Definition at line 56 of file rand.h.

◆ fc_randomly

#define fc_randomly (   _seed,
  _size 
)     fc_randomly_debug((_seed), (_size), "fc_randomly", __FC_LINE__, __FILE__)

Definition at line 73 of file rand.h.

◆ RANDLOG_OFF

#define RANDLOG_OFF

Definition at line 46 of file rand.h.

◆ RANDLOG_ON

#define RANDLOG_ON

Definition at line 45 of file rand.h.

Typedef Documentation

◆ RANDOM_TYPE

Definition at line 26 of file rand.h.

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