简体   繁体   中英

What's the Right Way to use the rand() Function in C++?

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.

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() .

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();
}

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

srand(time(NULL));

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM