--- title: "Rand_c" type: "article" slug: "rand_c" url: "http://localhost/article/rand_c/" markdown_url: "http://localhost/article/rand_c.md" published_at: "2025-11-20T07:14:11+00:00" modified_at: "2026-06-21T23:23:51+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "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…" category: - name: "QL Hacker's Journal" slug: "ql-hackers-journal" taxonomy: "category" url: "http://localhost/category/periodicals/ql-hackers-journal/" post_tag: - name: "C (programming language)" slug: "c-programming-language" taxonomy: "post_tag" url: "http://localhost/tag/c-programming-language/" - name: "Full Text" slug: "fulltext" taxonomy: "post_tag" url: "http://localhost/tag/fulltext/" - name: "QL" slug: "ql" taxonomy: "post_tag" url: "http://localhost/tag/ql/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" model: - name: "Sinclair QL" slug: "sinclair-ql" taxonomy: "model" url: "http://localhost/model/sinclair-ql/" indiv: - name: "Herb Schaaf" slug: "herb-schaaf" taxonomy: "indiv" url: "http://localhost/indiv/herb-schaaf/" - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" publication_r: id: 33686 title: "QL Hacker’s Journal" type: "periodical" url: "http://localhost/periodical/ql-hackers-journal/" authors_r: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" - name: "Herb Schaaf" slug: "herb-schaaf" taxonomy: "indiv" url: "http://localhost/indiv/herb-schaaf/" issue: "4" issues_articles: - id: 61540 title: "QL Hacker’s Journal 4" type: "issue" url: "http://localhost/issue/ql-hackers-journal-4/" pubdate: "July 1991" archive_link: true --- # Rand_c 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 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.