Reputation: 691
Is it possible? I'm basing my code on this one: http://cnds.eecs.jacobs-university.de/courses/osn-2004/s3.pdf
The thing I want to change is that I want to generate the east() and west() functions threads in a random way (one or the other) and at random times, because the code only generates the number of threads you insert on nw (threads going west) and ne (threads going east) variables in the main function. I'm not looking for any code because I haven't written mine and that code was written by another person, I just want an idea (cause I'm introducing myself into threads in C) of how I can use either random() or another function for accomplishing my goal. I still don't get the right idea on this thing. If you run the program with nw=3 and ne=2 for example, it'll print which cars are crossing the bridge and in which direction ( first 3 going west, then 2 going east). I want it to be more realistic, having random cars, arriving at the bridge at different times, so I can make a function that decides which car to go first like a traffic police. Sorry if I wasn't clear with the ideas. Any help will be useful
Upvotes: 0
Views: 103
Reputation: 99
In the entire world of Computer Science there is no thing that can replicate the properties of the "random" concept. The equivalent thing is a pseudo-random number generator.
In C there are different functions from the standard dedicated to that like rand().
The part about threading can be probably referred to the "thread spawning" topic.
Here it is a simple answer with a simple related codebase for example .
I personally think that you should switch to C++ for this kind of applications, especially if you are not even familiar with main topics you are in, with a particular look to the C++11 features.
Be aware that When talking about pseudo random number generation it's really important to use and code an algorithm that can do the job right, many are considering the C functions from the standard simply "broken" because there are problems in reproducing a pseudo-random behaviour in an effective way. So, depending on what you want, the functions from the standard couldn't be enough.
Upvotes: 1