Reputation: 788
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?
Upvotes: 18
Views: 24316
Reputation: 726569
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.
Upvotes: 12
Reputation: 106012
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);
}
Upvotes: 4
Reputation: 612954
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.
Upvotes: 27
Reputation: 8861
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.
Upvotes: 1
Reputation: 2892
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;
}
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).
Upvotes: 1
Reputation: 33273
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.
Upvotes: 8
Reputation: 409176
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.
Upvotes: 10