DPRG List

 DPRG: random numbers Message index sorted by: [ date ] [ thread ] [ subject ] [ author ] Previous message: DPRG: meeting reminder Next message: DPRG: Pain Subject: DPRG: random numbers From: David Philip Anderson dpa at io.isem.smu.edu Date: Mon Jan 12 16:40:47 CST 1998 ```howdy Here is a random number generator I've been using in my robots for a couple of years, coded in "C". I also have this running in Motorola HC6811 assembler, if that would be of use to anyone. The simplest is the irand() function, which returns signed 16 bit random integers between -32678 and 32767. This is probably good enough for most uses, although if you're doing image processing or some other function where you need thousands of random numbers, the periodicity becomes obvious. The rand() function uses the irand() code with a random table shuffle to improve the overall natural goodness of the random sequence, this one looks quite excellent when used for image processing or whatever. The last function uses the rand() code to generate a gaussian distribution. I have found it the most useful for robot behavior (i.e., slightly perturbing the amount of turn in an avoidance behavior to prevent the robot from being trapped in symetrical geometries. Also sounds pretty good in a random tone sequence...) The last two functions are floating point versions of the others. Hope this is useful, dpa /* --------------------------------------------------------------------- */ /* random.c random number generation 23 Feb 96 dpa Created. Fixed prime number. 04 Mar 96 dpa rand does >> 9 Version 2.0; Version 2.1; Made irand() call inline in rand(). Modified gauss to use longs, only one divide 27 Mar 96 dpa Version 2.2 Modified for HC6811 "IC" */ /* --------------------------------------------------------------------- */ /* 16bit linear congruential pseudo-random, -32768 < irand() < 32768 */ int iseed = 1234; int irand() { iseed = (iseed * 5431 + 1) & 0xffff; return iseed; } /* --------------------------------------------------------------------- */ /* improved non-periodic (less-periodic?) random table shuffle */ #define RNDSIZE 16 int rnd[RNDSIZE]; int rand_init() { int i; iseed = peekword(0x14); /* init with low-word of system clock */ for (i = 0; i < RNDSIZE; i++) rnd[i] = irand(); return 0; } /* --------------------------------------------------------------------- */ /* white noise */ int rand() { int i,j; j = (iseed * 5431 + 1) & 0xffff; i = (j >> 9) & 0xf; /* middle bits are more random */ iseed = rnd[i]; rnd[i] = j; return j; } /* --------------------------------------------------------------------- */ /* gaussian noise */ int gauss() { int g = rand()/4; g += rand()/4; g += rand()/4; g += rand()/4; return g; } /* --------------------------------------------------------------------- */ /* FLOAT VERSIONS */ /* --------------------------------------------------------------------- */ /* float versions, return -1.0 < random < 1.0 */ float frand() { float fr = (float) rand() / 32768.0; return fr; } /* --------------------------------------------------------------------- */ float fgauss() { float fg = (float) gauss() / 32768.0; return fg; } /* --------------------------------------------------------------------- */ /* EOF */ ------------------------------ ``` Previous message: DPRG: meeting reminder Next message: DPRG: Pain Message index sorted by: [ date ] [ thread ] [ subject ] [ author ] More information about the DPRG mailing list