tags:

views:

762

answers:

5

I'm doing a book exercise that says to write a program that generates psuedorandom numbers. I started off simple with.

#include "std_lib_facilities.h"

int randint()
{
    int random = 0;
    random = rand();
    return random;
}

int main()
{
    char input = 0;
    cout << "Press any character and enter to generate a random number." << endl;
    while (cin >> input)
    cout << randint() << endl;
    keep_window_open();
}

I noticed that each time the program was run, there would be the same "random" output. So I looked into random number generators and decided to try seeding by including this first in randint().

    srand(5355);

Which just generated the same number over and over (I feel stupid now for implementing it.)

So I thought I'd be clever and implement the seed like this.

srand(rand());

This basically just did the same as the program did in the first place but outputted a different set of numbers (which makes sense since the first number generated by rand() is always 41.)

The only thing I could think of to make this more random is to:

  1. Have the user input a number and set that as the seed (which would be easy to implement, but this is a last resort) OR
  2. Somehow have the seed be set to the computer clock or some other constantly changing number.

Am I in over my head and should I stop now? Is option 2 difficult to implement? Any other ideas?

Thanks in advance.

+18  A: 

Option 2 isn't difficult, here you go:

srand(time(NULL));

you'll need to include stdlib.h for srand() and time.h for time().

John T
+1, this is the standard practice.
TokenMacGuy
You can also read in /dev/random if you're in a *nix environment; but I agree with Token, this is a standard practice to set srand with the time function.
Suroot
Also, call srand() in main near the top since you should only call it once. Do not call it every time you generate a new number.
MahlerFive
The point here is that for a given seed, rand() will produce the same sequence of random numbers. So if you want a different sequence of random numbers each time your program runs, you need to provide a different seed on each run. That's the point of using the current time as the seed.
Keith Smith
Thanks this helped a lot!
trikker
+4  A: 

It is common to seed the random number generator with the current time. Try:

srand(time(NULL));

FigBug
+5  A: 

srand() should only be used once:

int randint()
{
    int random = rand();
    return random;
}

int main()
{
    // To get a unique sequence the random number generator should only be
    // seeded once during the life of the application.
    // As long as you don't try and start the application mulitple times a second
    // you can use time() to get a ever changing seed point that only repeats every
    // 60 or so years (assuming 32 bit clock).
    srand(time(NULL));
    // Comment the above line out if you need to debug with deterministic behavior.

    char input = 0;
    cout << "Press any character and enter to generate a random number." << endl;

    while (cin >> input)
    {
        cout << randint() << endl;
    }
    keep_window_open();
}
Martin York
I'll make sure to do this.
trikker
+4  A: 

The problem is that if you don't seed the generator it will seed itself with 0 (as if srand(0) were called). PRNGs are designed to generate the same sequence when seeded the same (due to the fact that PNRGs are not really random, they're deterministic algorithms and maybe a bit because it's quite useful for testing).

When you're trying to seed it with a random number using

srand(rand());

you're in effect doing:

srand(0);
x = rand();   // x will always be the same.
srand(x);

As FigBug mentioned, using the time to seed the generator is commonly used.

Michael Burr
Actually, the initial seed value is 1: "If `rand()` is called before any calls to `srand` are made, the same sequence shall be generated as when `srand` is first called with a seed value of 1." http://www.opengroup.org/onlinepubs/000095399/functions/srand.html
Daniel Trebbien
A: 

I think that the point of these articles is to have a go at implementing the algorithm that is in rand() not how to seed it effectively.

producing (pseudo) random numbers is non trivial and is worth investigating different techniques of generating them. I don't think that simply using rand() is what the authors had in mind.

Tim Jarvis