Rand_c

Publication

Pub Details

Date

Pages

See all articles from QL Hacker's Journal 4

In translating one of my pet programs from SuperBasic (first it started in Pascal), I needed a number of random numbers. Not finding a random number generator in Small-C, I decided to write the program in Lattice C. But, Lattice C does not really have any QL graphic functions. So back I go to Small-C to find a random number generator (RNG).

I tried a number of RNGs but had no luck. I was limited by Small-C handling only 16 bit integers with a range from -32K to 32K. Herb Schaaf found that floating point numbers go beyond the 16 bit limitation. Soon, he sent me a RNG and a test program. I took the RNG and turned it into a seperate C function and file that can be included into your C programs.

The seed for the RNG is taken from the date. This works well if you need a large number of random numbers. But if you run the program again, you will start with the same seed. If you need to have a program run numerous times, either build it into the code so it only initialises the RNG once, or have the user type in a seed at runtime.

/*  rand_c

For QL Small-C with floating point library.
Gets seed from date(). To make more random you
could call initrand() before every occurance of
rand().

USAGE:
int num1[3], temp[3], num2;

initrand(); * must call at least once *
*temp = float(100); * 100 in float *
fmove(rand(),num1); * put rand in num1 *
*num1 = fmult(num1,temp); * num1 * 100 *
num2 = int(num1);

*/

/*
Global variables used for functions
Small-C does not support Static Variables
*/

int rt, rnd[3], ra[3], rd[3], rm[3], rr[3];

initrand()
{
int t[2];

*rm = float(125);
*rd = atof("2796203");
*t = date();
*ra = float(t[1]);
}

rand()
{

*ra = fmult(ra,rm);
*ra = fdiv(ra,rd);
rt = int(ra);
*rr = float(rt);
*rnd = fsub(ra,rr);
*ra = fmult(rnd,rd);
return rnd;
}

Herb, not one to say “good enough”, has also come up with another RNG. This one is based on the algorithm that the ZX81 and T/S 2068 use in thier RNG’s. This program is not set up to be included in your program, but with some minor work can be made to do so.

/*  ZXrand_c    April 26, 1991 11am
* Using QL Small-C to
* Generate Random Numbers insame way as ZX-81 & TS 2068
*/

#include <stdio_h>


int a[3], b[3], m[3], d[3];
int seed[3], random[3], i;
char c, strg[6], *ow;

main() {
initz();
printf("Choose an integer seed in the range from 0 to 65535 \n");
gets(strg);
*seed = atof(strg);
printf("The seed will be %s\n",ftoa(seed,strg));
printf("touch [SPACE BAR] to begin, q to quit\n");
while((getc(c)) != 'q') {
rand();
printf("%6s",ftoa(seed,strg));
}
printf("\nPress any key to exit\n");
beep(10,1);
for(i=0; i<20480; i++) {
;
}
beep(10,2);
getc();
}

initz() {
*a = atof("65536");
*b = atof("65537");
*m = atof("75");
*d = atof("74");
}

rand() {
*random = fmult(m,seed);
*random = fadd(random,d);
*random = fmod();
fmove(seed,random);
}

fmod() {
while(fcmp(random,b) >0 ) {
*random = fsub(random,b);
}
return (*random);
}

_console() {
mode(4);
fopen(ow,"con","w");
window(ow,512,256,0,0);
paper(ow,0);
ink(ow,4);
border(ow,7,2);
cls(ow);
}

/* End of ZXrand_c Apr 26, 1991 noon ** */

Now that I know how to beat the 16 bit limit with floating point numbers, I have a few other RNGs that could be ported to Small-C. Plus, Herb still has a few more up his sleeves. If there is interest, I will publish these other RNGs.

Products

 

Downloadable Media

 
Scroll to Top