srand() and rand() Problem

Hi all,

I am writing an MFC App, and need to generate random numbers.
I know that rand() is not the best way, but it's ok for my program.

My problem is the following:

Even though i seed srand with the Systemtime at the intitialization of my program, every time I start the program, rand() draws the same numbers.

What is my mistake ? Any Ideas ?

Thanks in advance,

Corkwick

PS : My clock is working , of course.
[484 byte] By [Corkwick] at [2007-11-18 19:27:39]
# 1 Re: srand() and rand() Problem
ofcourse u'll get the same output because the rand() takes it seed at the first execution time but the srand(int) can chainge the output every time as in the code:
void main ()
{
cout<<srand(time(0));
}

or any thing like that,
ViperaPalestina at 2007-11-11 1:17:10 >
# 2 Re: srand() and rand() Problem
Hmm,

what can I do then ?
I need to draw random numbers in an interval of about 1000, and don't get different results, even though I seed srand with time(0).

Please help me :)

Thank you,

Corkwick
Corkwick at 2007-11-11 1:18:13 >
# 3 Re: srand() and rand() Problem
Your original description is the correct way to do it. There is probably a subtle bug in your code. Try...

/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void )
{
int i;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}

Code re-printed without any permission whatsoever from the MSDN help files...
TheCPUWizard at 2007-11-11 1:19:12 >
# 4 Re: srand() and rand() Problem
Thank you,

my srand looks just like that in the code.
I just tried to change the values, to some constants, and even remove srand.
Still I get the same results.
It seems, as if srand would, for some reason, do nothing in my program.
What could be wrong ?

Corkwick
Corkwick at 2007-11-11 1:20:14 >
# 5 Re: srand() and rand() Problem
Does the program I posted give you different numbers each time?

If so, then it is a bug in your program...
TheCPUWizard at 2007-11-11 1:21:13 >
# 6 Re: srand() and rand() Problem
Verify that your srand() statement is executed (ie put a breakpoint on it)... Then, if you're using threads, call srand() in every thread too (important).
j0nas at 2007-11-11 1:22:15 >