Is using too much srand(time(0)); bad?

In my code I used srand(time(0)); more than once.
In each of my functions I had it.
So is that bad? or if i use it once will it for ever randomly generate numbers even in a loop?
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    int x[100];
    for(int i = 0; i < 100; ++i) {
        std::srand(std::time(0));
        x[i] = std::rand() % 100;
    }
    for(int i: x)
        std::cout << i << ' ';
}
Executes in less than one ms on my machine, so time will always return single value.
Result you can see for yourself: http://ideone.com/zPHzFW

You should use srand() only once. And it is better to use C++11 random library: it is more random and powerful
> In my code I used srand(time(0)); more than once.
> In each of my functions I had it.
> So is that bad?

Yes. Time increases monotonically in a predictable manner; you won't get pseudo-randomness.

> if i use it once will it for ever randomly generate numbers even in a loop?

Not for ever. A pseudo random number generator has a cycle, after which it will repeat the same sequence of numbers all over again. For std::rand() the cycle would typically be about 231 or so; and std::mt19937 has a cycle of about 219937. These numbers are large and should be adequate for your requirements.

Prefer the C++11 random number library over std::rand()
You only need to use it once.
alright thank you all for this helpful information!
Topic archived. No new replies allowed.