# Construct your own random number generator

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.

5,557 Views

Scientist

``````// evaluation of three simple random number generators
// Written in Turbo C, modified for Pelles C
// 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

I like simpel stuff!

How would you implement this using the time as the seed?

To use the time as the seed is very simple.

Make sure you include the <ctime> header.

``#include <ctime>``

Then, inside ctime there is a function you can call like this:

``time(0);``

When you pass this method zero, it returns the time elapsed since January 1, 1970. This way you are guaranteed a differerent number each run time(if you run it less than 1 time per second).

Ok, so back to "how to make the time the seed" question. In these code examples replace the arbitrary number assigned to a with:

``static long a = long(time(0));``

Next, perform your random number generation using whichever method.

why in 1st method u have used value 100001 to intialize the varible a.is it a range of something.

It is one of the simplest logics, got it from a blog. in this logic you can limit the random numbers with that given modulus(%) operator inside the for loop,its just a copoy and paste from that blog, but any way check it out:

[

``````// random numbers generation in C++ using builtin functions
#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;

#include <cstdlib>   // contains function prototype for rand

int main()
{
// loop 20 times
for ( int counter = 1; counter <= 20; counter++ ) {

// pick random number from 1 to 6 and output it
cout << setw( 10 ) << ( 1 + rand() % 6 );

// if counter divisible by 5, begin new line of output
if ( counter % 5 == 0 )
cout << endl;

}

return 0;  // indicates successful termination

} // end main
- See more at: http://www.programmingtunes.com/generation-of-random-numbers-c/#sthash.BTZoT5ot.dpuf
``````
rubberman 1,355

Two problems. One is that this thread is 8 years old! Two is that writing good random number generators is not a trivial undertaking, and thinking it is just shows your ignorance of the subject. Read this: http://www.phy.ornl.gov/csep/CSEP/RN/RN.html

From the document, 2 relevant quotes:

Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin. -- John von Neumann (1951)

Anyone who has not seen the above quotation in at least 100 places is probably not very old. -- D. V. Pryor (1993)

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 learning and sharing knowledge.