GBDK 2020 Docs  4.3.0
API Documentation for GBDK 2020
rand.h File Reference
#include <types.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define RAND_MAX   255
 
#define RANDW_MAX   65535
 

Functions

void initrand (uint16_t seed) OLDCALL
 
uint8_t rand (void) OLDCALL
 
uint16_t randw (void) OLDCALL
 
void initarand (uint16_t seed) OLDCALL
 
uint8_t arand (void) OLDCALL
 

Variables

uint16_t __rand_seed
 

Detailed Description

Random generator using the linear congruential method

Author
Luc Van den Borre

Macro Definition Documentation

◆ RAND_MAX

#define RAND_MAX   255

◆ RANDW_MAX

#define RANDW_MAX   65535

Function Documentation

◆ initrand()

void initrand ( uint16_t  seed)

Initalise the pseudo-random number generator.

Parameters
seedThe value for initializing the random number generator.

The seed should be different each time, otherwise the same pseudo-random sequence will be generated.

One way to do this is sampling (DIV_REG) up to 2 times (high byte of seed value then the low byte) at variable, non-deterministic points in time (such as when the player presses buttons on the title screen or in a menu).

It only needs to be called once to be initialized.

See also
rand(), randw()

◆ rand()

uint8_t rand ( void  )

Returns a random byte (8 bit) value.

initrand() should be used to initialize the random number generator before using rand()

◆ randw()

uint16_t randw ( void  )

Returns a random word (16 bit) value.

initrand() should be used to initialize the random number generator before using rand()

◆ initarand()

void initarand ( uint16_t  seed)

Random generator using the linear lagged additive method

Parameters
seedThe value for initializing the random number generator.

Note: initarand() calls initrand() with the same seed value, and uses rand() to initialize the random generator.

See also
initrand() for suggestions about seed values, arand()

◆ arand()

uint8_t arand ( void  )

Returns a random number generated with the linear lagged additive method.

initarand() should be used to initialize the random number generator before using arand()

Variable Documentation

◆ __rand_seed

uint16_t __rand_seed
extern

The random number seed is stored in __rand_seed and can be saved and restored if needed.

// Save
some_uint16 = __rand_seed;
...
// Restore
__rand_seed = some_uint16;
uint16_t __rand_seed