[prev] 16 [next]

Sidetrack: Random Numbers (cont)

Seeding

There is one significant problem:

  • every time you run a program with the same seed, you get exactly the same sequence of 'random' numbers  (why?)
To vary the output, can give the random seeder a starting point that varies with time
  • an example of such a starting point is the current time, time(NULL)
    (NB: this is different from the UNIX command time, used to measure program running time)

    #include <time.h>
    time(NULL) // returns the time as the number of seconds
               // since the Epoch, 1970-01-01 00:00:00 +0000
    
    // time(NULL) on July 31st, 2020, 12:59pm was 1596164340
    // time(NULL) about a minute later was 1596164401