How do you use rand() to change as according to time?

Rand() itself is suppose to produce a random number but as i can see a computer will produce the same set of random numbers.
I heard that using rand() with some time thing can make it always be random.

How do you write it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand( time(0) );
    
    std::cout << "Randoming...\n";
    
    for( std::size_t i = 0; i < 100; ++i ) {
        std::cout << rand() << '\n';
    }
    
    std::cout << std::endl;
}


there is a new C++ header <random> which afaik produces true random numbers
Last edited on
Topic archived. No new replies allowed.