开发者

Same random numbers every time I run the program

My random numbers that output, output in the same sequence every time I run my game. Why is this happening?

I have

#include <cstdlib> 

and am开发者_如何学Go using this to generate the random numbers

randomDiceRollComputer = 1 + rand() % 6;


You need to seed your random number generator:

Try putting this at the beginning of the program:

srand ( time(NULL) );

Note that you will need to #include <ctime>.

The idea here is to seed the RNG with a different number each time you launch the program. By using time as the seed, you get a different number each time you launch the program.


You need to give the randum number generator a seed. This can be done by taking the current time, as this is hopefully some kind of random.

#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int  r;
    srand(time(0));
    r = rand();
    return 0;
} 


The rand() function is specifically required to produce the same sequence of numbers when seeded with a given seed (by calling srand()); each possible seed value specifies a sequence. And if you never call srand(), you get the same sequence you would have gotten by calling srand(1) before any call to rand().

(This doesn't apply across different C or C++ implementations.)

This can be useful for testing purposes. If there's a bug in your program, for example, you can reproduce it by re-running it with the same seed, guaranteeing that (barring other unpredictable behaviors) you'll get the same sequence of pseudo-random numbers.

Calling srand(time(NULL)) is the usual recommended way to get more or less unpredictable pseudo-random numbers. But it's not perfect. If your program runs twice within the same second, you'll probably get the same sequence, because time() (typically) has a resolution of 1 second. And typical `rand() implementations are not good enough for cryptographic use; it's too easy for an attacker to guess what numbers you're going to get.

There are a number of other random number implementations. Linux systems have two pseudo-devices, /dev/random and /dev/urandom, from which you can read reasonably high-quality pseudo-random byte values. Some systems might have functions like random(), drand48(), and so forth. And there are numerous algorithms; I've heard good things about the Mersenne Twister.

For something like a game, where you don't expect or care about players trying to cheat, srand(time(NULL)) and rand() is probably good enough. For more serious purposes, you should get advice from someone who knows more about this stuff than I do.

Section 13 of the comp.lang.c FAQ has some very good information about pseudo-random number generation.


Pseudorandom number generators take a starting number, or seed, and then generate the next number in the sequence from this. That's why they're called pseudorandom, because if they always use the same starting value, they will generate the same sequence of numbers like the C standard lib generator does. This can be fixed by giving the generator a starting value that will change the next time the program is run like the current time.

Anyway, the code you're looking for like others have said is:

srand(time(0)); //Seed the generator, give it a starting value
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜