In Las Vegas random numbers move billions of Dollars in and out of gamblers' pockets. For the mildly inquisitive here is a test of three simple generators.

# Construct your own random number generator

```
// evaluation of three simple random number generators
// Written in Turbo C, modified for Pelles C
// get Pelles C for free: http://smorgasbordet.com/pellesc/index.htm
// this C package comes with a great IDE
#include <stdio.h> // printf(), getchar(), puts()
// three different custom random number generators
int rand1(int lim);
int rand2(int lim);
int rand3(int lim);
int main(void)
{
int k, rnd1, rnd2, rnd3, sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
puts("Random numbers between 1 and 44:\n");
printf("%15s%10s %10s %10s","Custom functions","rnd1()","rnd2()","rnd3()");
for (k = 0; k < 40; k++) {
rnd1 = rand1(44); // method one
rnd2 = rand2(44); // method two
rnd3 = rand3(44); // method three
printf("\n%25d %10d %10d",rnd1,rnd2,rnd3);
sum1 += rnd1;
sum2 += rnd2;
sum3 += rnd3;
}
printf("\n\n%15s%10d %10d %10d (ideal 22)\n",
" Average:",sum1/k,sum2/k,sum3/k);
getchar(); // wait
return 0;
}
//
// returns random integer from 1 to lim
//
int rand1(int lim)
{
static long a = 100001;
a = (a * 125) % 2796203;
return ((a % lim) + 1);
}
//
// returns random integer from 1 to lim (Gerhard's generator)
//
int rand2(int lim)
{
static long a = 1; // could be made the seed value
a = (a * 32719 + 3) % 32749;
return ((a % lim) + 1);
}
//
// returns random integer from 1 to lim (Bill's generator)
//
int rand3(int lim)
{
static long a = 3;
a = (((a * 214013L + 2531011L) >> 16) & 32767);
return ((a % lim) + 1);
}
```

bumsfeld 413 Nearly a Posting Virtuoso

ultirian 0 Newbie Poster

nanodano 0 Junior Poster in Training

kirti4 0 Newbie Poster

irum.nageen.3 -6 Newbie Poster

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

nitin1 commented: those quotes are simple awesome!! :D +3

deceptikon commented: Yes. +12

JorgeM commented: very true.. nice.. +12

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.