rand.h (2233B)
1/** @file rand.h 2 Random generator using the linear congruential method 3 4 @author Luc Van den Borre 5*/ 6#ifndef RAND_INCLUDE 7#define RAND_INCLUDE 8 9#include <types.h> 10#include <stdint.h> 11 12/** Initalise the pseudo-random number generator. 13 14 @param seed The value for initializing the random number generator. 15 16 The seed should be different each time, otherwise the same pseudo-random 17 sequence will be generated. 18 19 The DIV Register (@ref DIV_REG) is sometimes used as a seed, 20 particularly if read at some variable point in time (such 21 as when the player presses a button). 22 23 Only needs to be called once to initialize, but may be called 24 again to re-initialize with the same or a different seed. 25 26 @see rand(), randw() 27*/ 28#if defined(__PORT_sm83) 29void initrand(uint16_t seed) OLDCALL; 30#elif defined(__PORT_z80) 31void initrand(uint16_t seed) Z88DK_FASTCALL; 32#endif 33 34#define RAND_MAX 255 35#define RANDW_MAX 65535 36 37/** The random number seed is stored in __rand_seed and can be 38 saved and restored if needed. 39 40 \code{.c} 41 // Save 42 some_uint16 = __rand_seed; 43 ... 44 // Restore 45 __rand_seed = some_uint16; 46 \endcode 47*/ 48extern uint16_t __rand_seed; 49 50/** Returns a random byte (8 bit) value. 51 52 @ref initrand() should be used to initialize the random number generator before using rand() 53 */ 54uint8_t rand() OLDCALL; 55 56/** Returns a random word (16 bit) value. 57 58 @ref initrand() should be used to initialize the random number generator before using rand() 59 */ 60uint16_t randw() OLDCALL; 61 62/** Random generator using the linear lagged additive method 63 64 @param seed The value for initializing the random number generator. 65 66 Note: initarand() calls @ref initrand() with the same seed value, and 67 uses @ref rand() to initialize the random generator. 68 69 @see initrand() for suggestions about seed values, arand() 70*/ 71#if defined(__PORT_sm83) 72void initarand(uint16_t seed) OLDCALL; 73#elif defined(__PORT_z80) 74void initarand(uint16_t seed) Z88DK_FASTCALL; 75#endif 76 77/** Returns a random number generated with the linear lagged additive method. 78 79 @ref initarand() should be used to initialize the random number generator before using arand() 80 */ 81uint8_t arand() OLDCALL; 82 83#endif