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