Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7943 Discussions

Correlation between seed and return value from 1st invokation of rand();

mishabear
Beginner
1,656 Views
The return value from first invocation of rand() seem correlates with the random seed value.

The value returned from first invocation of rand() after setting random seed by srand() changes only a 'little' from invocation to invocation. If a sequence of random number is generated rest values are ok.

Following program segment is compiled by Intel C++ v7.0 for win32, using VC++ 6.0, windows 2000sp3 and PIII 866.

#include
#include
#include
#include

using namespace std;

int main(int argc, char* argv[])
{
int i;

for(i=0; i<10000; i++)
{
srand((unsigned int)time(0));
cout << rand() << setw(10) << rand() << setw(10) << rand() << setw(10) << rand() << endl;
}
return 0;
}

0 Kudos
5 Replies
Ganesh_R_Intel
Employee
1,656 Views
mishabear,
Welcome!
But I guess that I dont fully understand the question here...

0 Kudos
mishabear
Beginner
1,656 Views
I write another program to demonstrate my problem. In the following program, a sequence of random number is generated. If a random number is bigger than half of RAND_MAX then a variable(K1) will be set otherwise another variable(K2) will be set. The program will not stop unless the sum of two variable(K1+K2) equlas to 2.

The program is expected to stop after runing a while, however this program does not stop actually.

I think this is a problem. I'm not sure who should be responsible for this problem, LIBC or compiler?

This program is complied by Intel VC 7.0 using VC 6.0 ,Win2KSp3 on PIII 866.


#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char* argv[])
{
int K1=0, K2=0;
size_t t1, t2;

t1 = time(0);
cout << "t1 = " << t1 << endl;
for(; (K1 + K2) < 2; )
{
srand(time(0));
if(rand() > (RAND_MAX / 2))
K1 = 1;
else
K2 = 1;
}
t2 = time(0);
cout << "t2 = " << t2 << endl;
cout << "K1 = " << K1 << endl << "K2 = " << K2 << endl << "t2 - t1 = " << (t2 - t1) << endl;
return 0;
}

0 Kudos
Maximillia_D_Intel
1,656 Views
It is unclear why you call srand repeatedly inside of the loop. Typically, you would call srand once say at the beginning of your program.

The rand function is a generator. If you call srand with a constant value, you should expect to see the same 'random' number sequence generated on repeated calls.

In your case, the random number sequence will change about once a second to coincide with your use of time(0) as the seed value.

If you move srand outside of the loop you should see better results.

Max

0 Kudos
mishabear
Beginner
1,656 Views
Thanks.

I see. It seems random generator must be 'warmed up' before put to use.
Is this a standard or implementation feature of random generator?

0 Kudos
Maximillia_D_Intel
1,656 Views
Yes. Do a search for random number generation on the web and you can get more details. Essentially the core of random number generation (used in drand()) relies upon a generator. I would bet that if you played with it a bit, you should find that your 'random number' sequence would repeat itself after generating a large number of random numbers.

Max
0 Kudos
Reply