![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 154
Rep Power: 3
![]() |
c-unix-childprocesses-random number
well, the parent process is supposed to fork out two child processes and each of the child process are supposed to come up with a random integer between 0 and 1 and do a lot of other things
however, each child process is supposed to do identical things, therefore, i wrote the same function (say functionc()) for both of them and just passed appropriate parameters to them. I am using rand () to come up with the random number in functionc() [HTML]int choice = rand() % 2;[/HTML] now the problem is both child process always come up with the same random number. In other words, either both child processes will come up with 1 or both child processes will come up with 0 ... I am guessing part of the problem is they are using exactly identical memory images, so even the random number generator is generating the same number for both of them? is there any way i fix that apart from writing different functions for both of them, which doesnt so sound sophiscated when both of them are supposed to do exactly same thing except come up with different random number occassionalyy |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 154
Rep Power: 3
![]() |
oh btw I tried
[HTML]srand(getpid()); int choice = rand() % 2; // random integer - 0 and 1 [/HTML] the problem will this is it will ALWAYS alternate LOL lol i want something that will alternate sometimes, sometimes it wont ... something like that |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
There's no such thing as an integer between 0 and 1. Google 'integer'.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
srand(time(NULL) % getpid() );
And DaWei is correct. You want int choice= rand()%2; choice ++; This gives 1 or 2. If you really want .0 ... .999999 then double choice=rand(); double next=rand(); choice=(next > choice) ? choice/next : next/choice; or use something like drand48(); |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 154
Rep Power: 3
![]() |
|
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
well you could perform some calculations with PID, such as add to how it many milliseconds have passed.
__________________
Quote:
|
|
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 154
Rep Power: 3
![]() |
Quote:
because I called srand function inside the while loop i called it outside the while loop and now it gives pretty random results thanks folks |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Yup. Seed it just once. If you seed it with the same value more than once, you'll get a repeat of the "random" numbers.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Median/Mode in arrays? {Need help} | Java|Tera | Java | 27 | Nov 29th, 2005 10:50 AM |
| FiveDigit + RandomeNumber Game. | TecBrain | Java | 0 | Nov 18th, 2005 2:53 PM |
| Random Number & Average Problem | Hadrurus | Java | 6 | Aug 15th, 2005 1:08 PM |
| generate a random number | cwl157 | Java | 3 | Apr 15th, 2005 6:18 PM |
| non repeating random number generation | gencor45 | C# | 2 | Feb 9th, 2005 12:11 AM |