As OriginalGriff has pointed out, the chances of calling the time_t within a single second are quite high. For a point of reference, consider the following program
#include <iostream>
#include <chrono>
using Clock = std::chrono::steady_clock;
using Seconds = std::chrono::seconds;
int main()
{
const size_t nsec = 2;
size_t ncalls;
auto start = Clock::now();
auto elapsed = [start]() {
return std::chrono::duration_cast<Seconds>(Clock::now() - start).count(); };
for(ncalls = 0; elapsed() < nsec; ++ncalls)
time(0);
std::cout << "performed " << ncalls/nsec << " calls per second\n";
}
Running this on my desktop PC, I get over 23,000,000 calls per second. Even a pi-zero will generate about 375,000 calls per second. This is doing far more than just a single call to
time()
per loop, so the actual number of system calls is at least 2 times the value printed, perhaps even 3 or 4 times.
So its probably not a good idea to use
time()
to generate a unique number. You could use the time() call as a seed to the standard C random number generator (srand()/rand()). Or you could look into
Pseudo-random number generation - cppreference.com[
^]