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 00:58

If you don't seed the generator, it will have the same seed every time you run your program, and the random number sequence will be the same each time.

Also note that you only should to seed the generator once, at the beginning of the program.

查看更多
家丑人穷心不美
3楼-- · 2020-02-01 01:01

In C/C++, a dice roll would be simulated like this:

int face = (rand() % 6) + 1);
                   ^
                   |___________ Modulo operator

The % 6 limits the random number to 0 through 5 and the + 1 is made to offset the limit, so it becomes 1 through 6.

查看更多
Juvenile、少年°
4楼-- · 2020-02-01 01:02

The seed is needed for pseudo random number generator to generate different random number sequence from previous ones (not always). If you do not want the repeated sequence then you need to seed the pseudo random number generator.

Try these codes and see the difference.
Without seed:

#include <stdio.h>
#include <stdlib.h>

int main()
{
        printf("%d", rand()%6 + 1);
}  

With seed:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
        srand(time(NULL));
        printf("%d", rand()%6 + 1);
}
查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-01 01:04

What's usually called a random number generator is actually a pseudo-random number generator. This typically means that you can generate the same random sequence if you provide the "key" to that sequence, referred to as the "seed". This is very useful when you wish to test your algorithm that is based on randomization, and you need to ensure repeatable results.

If you do not "seed" your Random number generator, it is seeded with some (usually based on system time) random number by default, and therefore produces the different sequence every time that you run your program.

查看更多
Explosion°爆炸
6楼-- · 2020-02-01 01:08

Given the same seed, a pseudo random number generator will produce the same sequence every time. So it comes down to whether you want a different sequence of pseudo random numbers each time you run, or not.

It really depends on your needs. There are times when you want to repeat a sequence. And times when you do not. You need to understand the needs of each specific application.

One thing you must never do is seed repeatedly during generation of a single sequence. Doing so very likely will destroy the distribution of your sequence.

查看更多
做个烂人
7楼-- · 2020-02-01 01:13

The random number generator is not truly random: say you seed it with 12 and make 100 random numbers, repeat the process and seed it with 12 again and make another 100 random numbers, they will be the same.

I have attached a small sample of 2 runs at 20 entries each with the seed of 12 to illustrate, immediately after the code which created them:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    srand(12);
   for (int i =0;i<100; i++)
   {
       cout << rand() << endl;
   }
   return 0;
}

two randomly generated sequences both seeded with 12

To avoid such repetition it is commonplace to use a more unique value, and as the time is always changing, and the chances of a two programs generating random sequences at exactly the same time are slim (especially when at the millisecond level), one can reasonably safely use time as an almost unique seed.

Requirements: seeding only need take place once per unique random sequence you need to generate.

However, there is an unexpected up-/down-side to this: if the exact time is known of when the first sequence is generated then the exact sequence can be re-generated in the future by entering the seed value manually, causing the random number generator to step through its process in the same fashion as before (this is an upside for storing random sequences, and a downside for preserving their randomness).

查看更多
登录 后发表回答