diff options
author | Matt Turner <mattst88@gmail.com> | 2008-11-11 23:17:11 +0000 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2008-11-11 23:17:11 +0000 |
commit | 974755deb9bbea37752d2202844a1f485e59c8e7 (patch) | |
tree | 6035e96dab509aba4f535fba25a9c58a29045ad3 /randnum.c | |
parent | 4400334ffbffba2462d7e049c79907ff0b148a52 (diff) |
-- Rename misc.c randnum.c
-- Delete misc.h
git-svn-id: svn://mattst88.com/svn/cleanbench/trunk@8 0d43b9a7-5ab2-4d7b-af9d-f64450cef757
Diffstat (limited to 'randnum.c')
-rw-r--r-- | randnum.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/randnum.c b/randnum.c new file mode 100644 index 0000000..7d2620f --- /dev/null +++ b/randnum.c @@ -0,0 +1,55 @@ +#include <stdint.h> +#include <math.h> + +/**************************** +** RANDOM NUMBER GENERATOR ** +***************************** +** This is a second-order linear congruential random number +** generator. Its advantage is (of course) that it can be +** seeded and will thus produce repeatable sequences of +** random numbers. +*/ + +/**************************** +* randnum() * +***************************** +** Second order linear congruential generator. +** Constants suggested by J. G. Skellam. +** If val==0, returns next member of sequence. +** val!=0, restart generator. +*/ + +int32_t randnum(int32_t val) { + static int32_t randw[2] = { 13 , 117 }; + int32_t interm; + + if (val != 0) { + randw[0] = 13; + randw[1] = 117; + } + + interm = (randw[0]* (int32_t)254754 + randw[1]*(int32_t)529562) % (int32_t)999563; + randw[1] = randw[0]; + randw[0] = interm; + return(interm); +} + +/**************************** +* randwc() * +***************************** +** Returns signed 32-bit random modulo num. +*/ +int32_t randwc(int32_t num) { + return(randnum(0) % num); +} + +/*************************** +** abs_randwc() ** +**************************** +** Same as randwc(), only this routine returns only +** positive numbers. +*/ +uint32_t abs_randwc(uint32_t num) { + return abs(randwc(num)); +} + |