Originally Posted by
Eldarion
I do something completely different-
I include <cstdlib>, which has a rand() function, and include<ctime>.
I then use the clock() function to execute rand() many times based on how many clock ticks have passed since the program started, which puts me into an entirely random place in the rand()'s random number list.
I had no idea about srand() and all that.
Anyway; cstdlib's rand() doesn't go up in value every time, and a way of making a reliable min and max is by dividing the random number by RAND_MAX, multiplying by (MAX-MIN) and adding MIN.
Are you sure there is any guarantee that clock() will return values that are random enough.
Anyway, if you keep giving ascending values to srand, there's no surprise that you might get ascending values from the next call to rand(). The whole point of rand() is that there is a long-long randomish mathematical sequence and srand() determines your starting point in the sequence.
This doesn't mean that the first number you get after seeding is particularly unpredictable, e.g
Code:
#include <cstdio>
#include <cstdlib>
int main()
{
for (int seed = 32; seed <= 42; ++seed) {
srand(seed);
printf("%d %d\n", seed, rand());
}
}
32 143
33 146
34 149
35 152
36 156
37 159
38 162
39 165
40 169
41 172
42 175
Also see how the (pseudo)randomness kicks in if you draw more random numbers from a seed:
Code:
#include <cstdio>
#include <cstdlib>
int main()
{
for (int seed = 32; seed <= 42; ++seed) {
srand(seed);
printf("%d", seed);
for (int i = 0; i != 8; ++i) {
printf(" %6d", rand());
}
puts("");
}
}
Code:
32 143 23988 3067 18799 11241 6380 9660 4148
33 146 1968 20932 10094 21556 10307 12772 1221
34 149 12717 6028 1389 31870 14234 15884 31062
35 152 23465 23892 25453 9417 18160 18997 28135
36 156 1446 8988 16748 19731 22087 22109 25207
37 159 12194 26852 8043 30046 26014 25222 22280
38 162 22942 11948 32107 7593 29941 28334 19353
39 165 923 29813 23402 17907 1099 31447 16426
40 169 11671 14909 14697 28222 5026 1791 13498
41 172 22420 5 5992 5768 8953 4904 10571
42 175 400 17869 30056 16083 12879 8016 7644