By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

random() and fork()

P: n/a
Hello
I m trying to having X child process from 1 original process to generate
random numbers.

I m using (1+(int) (10.0*rand()/(RAND_MAX+1.0))) from the man page of
rand() /random().

The problem is that all these childs give as result the absolutly same
value.

Why ? Is it possible to have X forked childs giving different random
values ?

Thanks in advance

Axel

Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Axel <ax************************@netcourrier.com> scribbled the following:
Hello
I m trying to having X child process from 1 original process to generate
random numbers. I m using (1+(int) (10.0*rand()/(RAND_MAX+1.0))) from the man page of
rand() /random(). The problem is that all these childs give as result the absolutly same
value. Why ? Is it possible to have X forked childs giving different random
values ?


Without delving into the platform-specific process-spawning stuff, I
think it's most likely that all your children are getting seeded with
the same seed, thus the PRNG (pseudo-random number generator) generates
the same pseudo-random numbers. Look up srand().

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
Nov 13 '05 #2

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following:
Axel <ax************************@netcourrier.com> scribbled the following:
Hello
I m trying to having X child process from 1 original process to generate
random numbers. I m using (1+(int) (10.0*rand()/(RAND_MAX+1.0))) from the man page of
rand() /random(). The problem is that all these childs give as result the absolutly same
value. Why ? Is it possible to have X forked childs giving different random
values ?
Without delving into the platform-specific process-spawning stuff, I
think it's most likely that all your children are getting seeded with
the same seed, thus the PRNG (pseudo-random number generator) generates
the same pseudo-random numbers. Look up srand().


....noting, after I posted this, the rather funny occurrence of writing
"children are getting seeded"... =)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.