Random Number Generator (PC Magazine Vol 2 No 5 Oct 1983 User-to-User) Random numbers are used extensively in computing for games, simulations, and Monte Carlo analysis techniques. The C language random number generator below for the IBM PC may be of interest. It is a self-seeding function, which performs as follows. A long sequence of random numbers between 0 and 1 can be expected to have an arithmetic mean of 0.5 and a standard deviation of 0.2887. Repeated 10,000 iteration tests have consistently yielded results within 0.01 of these theoretical values. Repeated 1,000 iteration tests for duplication using integer truncation have shown a duplication rate of 5 to 8 percent at 4 decimal places, 2 to 3 percent at 5 decimal places, and 1 to 3 percent at 6 decimal places. A one-line program which calls the function 1,000 times and assigns the value to a floating-point variable executes in 9.2 seconds. The timing may be of concern in real-time programs. The function is straightforward. It maintains two static integers, the seed and a counter which is initialized to zero and incremented on each function call. The first time it is called (and the 65,536th time thereafter), it obtains a seed from the low word of the system timer (a source of pseudorandom numbers itself). It does this through the C library function "sysint," a system interrupt function, which provides the C programmer with access to the ROM BIOS routines from within C. On subsequent calls, it adds the call count to the existing seed, a step which ensures that, if the seed ever became zero, the function would not lock at zero output. While this step (and the count variable) can be eliminated by initializing the seed to zero and testing for a zero seed to initiate the interrupt, the statistics for that function are not as good as they are for this one. The function next generates a double precision product of the seed and pi, which it splits into its integral and fractional parts using another C library function, modf. The double-precision integral part is cast into an integer representation with an acceptable (for this application) loss of digits if it is too large to fit, for use as the next seed. The fractional part is returned as the random number. The sign check before returning is necessary as the library version 1.33 modf function returns a signed decimal value. Random Number Generator in C Language #define PI 3.14159265358977 struct regval {int ax, bx, cx, dx, si, di, ds, es;}; double rnd() /* random number generator 1 */ { static unsigned int seed, count=0; double ranum, nextseed; double modf(); /* splits int & fract parts */ if ( count==0 ) { /* get seed from system timer */ struct regval srv; srv.ax=0; sysint(0x1A, &srv, &srv); seed=srv.dx; } /* here it is */ else seed+=count; ranum=seed*PI; ranum=modf(ranum, &nextseed); seed=(int)nextseed; ++count; if (ranum>=0.0) return ranum; else return -ranum; } /* end random function */ ----------------------------------------------------------------- Another Random Number Method (PC Magazine Vol 2 No 6 Nov 1983 User-to-User) Here's another, possibly better, way of creating a "random random" number series. Include in your BASIC program: 500 RANDOMIZE VAL(MID$(TIME$,4,2)) *100+VAL(MID$(TIME$,7,2)) This statement uses the time of day as the seed for the BASIC random number generator. In principle, it is no different from the method suggested in User-to-User July 1983; it simply uses the PC's internal clock as the counter. The advantage is that only a single program line is required and no variables are defined. Programs that are often booted and automatically started may not work well with this method if the RANDOMIZE statement is too close to the top of the program, particularly when they are run on PCs without a battery-operated clock. This is because, in the absence of a battery- operated clock, the PC's time of day is reset every time the system is booted. The value of the TIME function, therefore, will tend to be the same number from run to run unless the RANDOMIZE function appears after an on-screen question asked of the user.