18

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?

user2991252
  • 760
  • 2
  • 11
  • 28
  • 14
    A note: you shouldn't seed it every time a particular piece of code is run; you should seed it every time the process starts. – Roger Lipscombe Dec 17 '13 at 14:34
  • 1
    @RogerLipscombe It really depends on what you are trying to achieve. – juanchopanza Dec 17 '13 at 14:50
  • The [wikipedia](http://en.wikipedia.org/wiki/Pseudorandom_number_generator) article is pretty good. In Java it still seeds the PRNG at the first call, but it's [under the hood.](http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#random()) – kbshimmyo Dec 17 '13 at 15:16

7 Answers7

27

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • What is the meaning of seed ? I searched google and most links give no explanation for it. – Erran Morad Aug 03 '14 at 05:57
  • "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." Could you explain why? It seems to me that seeding each time will just make it 'more random'. – Jose V Aug 26 '17 at 21:50
  • @Jose https://stackoverflow.com/questions/976993/issues-with-seeding-a-pseudo-random-number-generator-more-than-once https://www.johndcook.com/blog/2016/01/29/random-number-generator-seed-mistakes/ – David Heffernan Aug 28 '17 at 09:31
12

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.

jmroyalty
  • 2,527
  • 1
  • 17
  • 21
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
10

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I thought it was the opposite that if we seed the generator, then we will get the same random sequence each time? – user5965026 Apr 24 '20 at 17:22
  • @user5965026 The idea is to use a *unique* seed. That's why it's so common to use the result of `time`, which usually returns a value with second-resolution. – Some programmer dude Apr 24 '20 at 19:50
8

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.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • 4
    This is very useful for debugging codes that involve random number generation. – kbshimmyo Dec 17 '13 at 15:09
  • Indeed. If you want mostly unpredictable seeds but the ability to reproduce a behavior you observed, have your program display/log the seed used along with the results. And add a command-line switch to specify the seed, using it instead of `time(NULL)` if present. – Medinoc Dec 17 '13 at 16:35
  • This is also handy for simulation. – joshin4colours Dec 17 '13 at 16:53
4

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);
}
haccks
  • 104,019
  • 25
  • 176
  • 264
1

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.

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
1

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

GMasucci
  • 2,834
  • 22
  • 42