Re: A89: Random Number generator in C
[Prev][Next][Index][Thread]
Re: A89: Random Number generator in C
Here is one set of routines which are not bad, adapted from
"Learning to program in C" by Thomas Plum (they require
linking with longmul.o):
static long __seed = 1;
int rand(void) // Returns random number in range 0-32767
{
return ((__seed=0x41c64e6d*__seed+0x3039)>>8)&0x7FFF;
}
#define random(x) ((int)((long)(unsigned)rand()*(unsigned)(x)/32768))
// Returns random number in range 0 - x
#define srand(x) ((void)(__seed = (x)))
// Sets the seed for random number generator
#define randomize() ({if(__seed==1) srand(*(char*)0x600017);})
// Initializes seed randomly, as I used in my programs