Rand() - srand detailed explanation please?

Hi, need more info about srand, course says we need to use time of day to “shuffle the cards”, but I simply don’t get it:

srand(time(NULL))

how this code line tells to generate a random number based on time of day, every time it reads rand()?

I mean, time of day is a different data type (I assume), how it actually tells rand to pick a different number set (seed let’s say) each time?

and above that, rand is supposed to generate a randomized number on each cycle, why do we need a different seed to change numbers?

1 Like

srand() seeds the pseudo-random number generator used by rand() . If rand() is used before any calls to srand() , rand() behaves as if it was seeded with srand(1) . Each time rand() is seeded with srand() , it must produce the same sequence of values.

- https://en.cppreference.com/w/c/numeric/random/rand

To continue with the deck of cards analogy. Every time you call rand you just get the next “random” number by taking a card off the top of the deck, reading it, then putting it at the bottom. So without seeding rand you’ll always be using a pre-shuffled deck of cards that’s in the same order e.g.

Ace of Clubs -> Two of Clubs -> Three of Clubs

And if you restart the program you will get the exact same sequence.

For testing purposes using a known seed (e.g. srand(3)) is great as it makes testing far easier. However it makes for a terrible random number generator afterwards.


time is a function from C

Returns the current calendar time encoded as a std::time_t object, and also stores it in the object pointed to by arg , unless arg is a null pointer.

- https://en.cppreference.com/w/cpp/chrono/c/time

In practice that is the number of seconds since January 1st 1970. So with that, every time you run the program you would be seeding rand with a different number… unless you manage to run the program multiple times within the same second.

2 Likes

Great explanation. So if I got it straight;

rand actually does not “randomize” or randomly choose a number from a given set, but every time it s called, it exactly chooses same numbers. In order to make it generate random numbers, we need to change the set it uses every time (or shuffle the same set), and that’s what srand does.
So we have to use rand in conjunction with srand to get real randomized outcome and we use time of day (in seconds) to get different seed numbers to put a shuffled deck in front of rand.

I think I got the idea…

…in the sequence.

That would be the combination of srand and time does. You can give a fixed seed and it will be the same sequence each time e.g.

#include <cstdlib>
#include <iostream>

int main()
{
    srand(10);
    for(int i = 0; i < 10; ++i)
    {
        std::cout << rand() << '\n';
    }
}

On my machine this will always output

71
16899
3272
13694
13697
18296
6722
3012
11726
1899

no matter how many times I run the program. Changing to srand(1) (same if you don’t call srand before using rand) will output

41
18467
6334
26500
19169
15724
11478
29358
26962
24464
1 Like

Ok, perfectly understood. Thanks for the help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms