Okay, I need to somehow use the fork function wich creates child processes to somehow generate six random numbers from 1-50. All I've managed so far is to generate numbers from 1-6. Anyone can give me a hand?
Any good reason you're using fork for this?
I guess you just grab the pid value returned to the parent process. How come you've only gotten 1-6?
You should really tell us everything if you want useful feedback.
I am using fork because I'm supposed to. I know srand would have made my life much easier, but I am somehow supposed to do this with fork. That's the best thing I could figure, the pid thing. That's what I try, I still get 1-6.
I assume this is for a homework assignment. Is fork() supposed to be the actual source of randomness (that would be very strange), or is the intent for the child process to choose random numbers using some normal means like rand(), and communicate them back to the parent process to be displayed to the user?
I am not sure what exactly we are supposed to do. The exercise says that we are supposed to use somewhere fork. I don't think we are supposed to use rand because the professor told us to use this way of randomly generating numbers (the fork thing) on another exercise too. And I asked him why can't we just use rand and he said that not everyone knew how rand works, so he told us to use the same way as a hint(supposing we had dealt with the fork exercise).
If rand isn't explicitly forbidden, could you seed the random number generator with the pid of the application and then return ( rand() + 1 ) % 50; (Although using the modulus operator here is a bad habit - the randomness isn't really random...)
[editline]01:39AM[/editline]
Because fork can't generate random numbers. PIDs are even less random than the numbers you get from rand.
You should talk to your professor to clarify what the purpose of the assignment is.
Forking and having the child pass values back to the parent teaches useful lessons about basic process management and IPC, and I've seen similar assignments elsewhere.
Using fork() to generate "random" numbers, on the other hand, is nonsensical and doesn't teach anything useful. I really doubt that's what your professor wants you to do.
If it is, find a better professor. :)
[QUOTE=esalaka;23340165]If rand isn't explicitly forbidden, could you seed the random number generator with the pid of the application and then return ( rand() + 1 ) % 50; (Although using the modulus operator here is a bad habit - the randomness isn't really random...)
[editline]01:39AM[/editline]
Because fork can't generate random numbers. PIDs are even less random than the numbers you get from rand.[/QUOTE]
That's pretty much what I wanted made clear. You can't generate random numbers with fork. So it must be forking with rand. I ll give this a try. Thanks, people.
Sorry, you need to Log In to post a reply to this thread.