1,105,581 Community Members

Construct your own random number generator

Member Avatar
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
 
1
 

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.

// 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);
}
Member Avatar
bumsfeld
Posting Virtuoso
1,537 posts since Jul 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 261 [?]
Skill Endorsements: 7 [?]
 
1
 

I like simpel stuff!

Member Avatar
ultirian
Newbie Poster
8 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
nanodano
Junior Poster in Training
78 posts since Feb 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
kirti4
Newbie Poster
1 post since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
irum.nageen.3
Newbie Poster
16 posts since Sep 2013
Reputation Points: -6 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-1
 

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 
Member Avatar
rubberman
Senior Poster
3,997 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
4
 

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)

You
Post:
Start New Discussion
Tags Related to this Article