In order to generate a sequence of [pseudorandom][1] numbers, the generator needs to be [seeded][2]. The seed fully determines the sequence of numbers that will be produced. In C, you seed with `srand`, as you indicate. According to the `srand(3)` man page, no explicit seeding implies that the generator will use 1 as a seed. This expains why you always see the same numbers (but do remember that the sequence itself is pretty random, with quality depending on the generator used, even though the sequence is the same each time).
User mzabsky points out that one way to get a seed that feels random to a human user is to seed with time. Another common method (which I just saw that mzabsky also points out - sorry) is to seed the generator with the contents of the system's random number generator, which draws from an entropy pool fed by things such as mouse movement, disk timings etc. You can't draw a lot of randomness from the system generator, as it won't be able to gather enough entropy. But if you just draw a seed from it, you'll have chosen at random a sequence of random numbers in your program. Here's an example of how to do that in C on Linux:
unsigned int seed;
FILE* urandom = fopen("/dev/urandom", "r");
fread(&seed, sizeof(int), 1, urandom);
fclose(urandom);
srand(seed);
In light of Conrad Meyer's answer, I thought I'd elaborate a bit more. I'd divide the use of random numbers into three categories:
1. **Variation.** If you use random numbers to create seemingly random or varied behavior in for example a game, you don't need to think very hard about the topic, or about choosing a proper seed. Seed with time, and look at some other solution if this turns out not to be good enough. Even relatively bad RNGs will look random enough in this scenario.
2. **Scientific simulations.** If you use random numbers for scientific work, such as Monte Carlo calculations, you need to take care to choose a *good generator*. Your seed should be fixed (or user-changeable). You *don't* want variation (in the sense above); you want deterministic behavior but good randomness.
3. **Cryptography.** You'll want to be extremely careful. This is probably out of the scope of this thread.
[1]:
[To see links please register here]
[2]:
[To see links please register here]