Different seeding of srand() during a SINGLE program run

B

bobrics

Hi,
I am using srand() and would like to create different random numbers
during a SINGLE execution of my program because I want to compare
random cases.
For now I have a switch statement within a loop, which does not do the
job. I get the same output from my simulator every time. Does it mean
that it actually can seed ONCE per program run?
Thanks


for(i=....; i++){
switch (i){
case 1: seed = 10; break;
case 2: seed = 20; break;
default: printf("error"); break
}
srand(seed);

//code that uses Random()
}
 
J

Jack Klein

Hi,
I am using srand() and would like to create different random numbers
during a SINGLE execution of my program because I want to compare
random cases.
For now I have a switch statement within a loop, which does not do the
job. I get the same output from my simulator every time. Does it mean
that it actually can seed ONCE per program run?
Thanks

No, you can use srand() to seed rand() as many times as you like in a
program.
for(i=....; i++){
switch (i){
case 1: seed = 10; break;
case 2: seed = 20; break;
default: printf("error"); break
}
srand(seed);

//code that uses Random()
}

What is Random()? It is not a standard C function. It does whatever
its documentation says it does, and we have no idea whether it uses
the seed set by srand() or not.

How does your reseeding of srand() work when you use rand() to
retrieve values?
 
A

Alexei A. Frounze

bobrics said:
Hi,
I am using srand() and would like to create different random numbers
during a SINGLE execution of my program because I want to compare
random cases.
For now I have a switch statement within a loop, which does not do the
job. I get the same output from my simulator every time. Does it mean
that it actually can seed ONCE per program run?

The pseudo-random number generator that is implemented in most of the
standard libraries is purely deterministic. If you want different sequences
of numbers from a call to call, you must initialize the seed differently
every time. If from a run to run the inital seed is the same, the sequences
will be the same too. Perhaps, all you need is to initialize with something
like current time (since e.g. epoch or computer power up) in seconds, e.g.
use time(NULL) return value...

Alex
 
B

bobrics

thank you guys for all the replies.

I have made a mistake in some other place, which caused the code under
test to execute once, even though it was run several times. So, it
created an illusion that my seeder didn't do the job properly. Well,
two lessons are learned here. First, is that srand() can generate
different numbers within the same run, as expected! : )
And second one is that when you take a code and think of it as a
blackbox and plan to run it several times with different parameters -
do not forget to reset ALL variables to initial values, and not only
the ones under test!

Leave and learn,
Thank you for all the replies again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top