GBDK 2020 Docs  4.1.1
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 () OLDCALL
 
uint16_t randw () OLDCALL
 
void initarand (uint16_t seed) OLDCALL
 
uint8_t arand () 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.

The DIV Register (DIV_REG) is sometimes used as a seed, particularly if read at some variable point in time (such as when the player presses a button).

Only needs to be called once to initialize, but may be called again to re-initialize with the same or a different seed.

See also
rand(), randw()

◆ rand()

uint8_t rand ( )

Returns a random byte (8 bit) value.

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

◆ randw()

uint16_t randw ( )

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

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