Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 6th, 2007, 6:01 AM   #1
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
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
programmingnoob is offline   Reply With Quote
Old Feb 6th, 2007, 6:13 AM   #2
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
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
programmingnoob is offline   Reply With Quote
Old Feb 6th, 2007, 8:17 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 6th, 2007, 3:56 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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();
jim mcnamara is offline   Reply With Quote
Old Feb 6th, 2007, 4:35 PM   #5
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by DaWei View Post
There's no such thing as an integer between 0 and 1. Google 'integer'.
lol i am sorry, i worded it poorly

i meant either 0 and 1, not "between 0 and 1"

sorry
programmingnoob is offline   Reply With Quote
Old Feb 6th, 2007, 4:50 PM   #6
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
well you could perform some calculations with PID, such as add to how it many milliseconds have passed.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Feb 6th, 2007, 6:29 PM   #7
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by programmingnoob View Post
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
i know why it always alternates ....
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
programmingnoob is offline   Reply With Quote
Old Feb 6th, 2007, 9:39 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 11:50 AM
FiveDigit + RandomeNumber Game. TecBrain Java 0 Nov 18th, 2005 3:53 PM
Random Number & Average Problem Hadrurus Java 6 Aug 15th, 2005 2:08 PM
generate a random number cwl157 Java 3 Apr 15th, 2005 7:18 PM
non repeating random number generation gencor45 C# 2 Feb 9th, 2005 1:11 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC