Random number generator - why seed every time

2020-02-01 00:28发布

I am relative new to c and c++. In java, the language I am used to program in, its very easy to implement random number generation. Just call the the static random-method from a class called Math.

int face = ((int)(Math.random() * 6) + 1);

simulates a dice-throw ...

In c and c++ you have to "seed the random number generator" , by calling the srand-function

srand ( time(NULL) );

What is the point of doing this - I mean is there any advantage of having to seed the random number generator every time the code is run?

标签: c++ c
7条回答
2楼-- · 2020-02-01 01:14

The advantage is that you can repeat a random number sequence by supplying the same seed.

The game Elite used this to store an entire world, consisting of thousands of stars, as a single number. To generate the exact same world a second time, the just supplied the same seed.

查看更多
登录 后发表回答