Hi, I don't understand the need for the while statement. Any help would be greatly appreciated.

double michel()
{
	double x, y;
	do                                // The loop computes points on the graph of y against x.
	{
		x = 53.0*rand()/RAND_MAX;     // x is the momentum. x is randomly generated. Min x is 0 MeV/c. Max x is 53 MeV/c.
		y = x/53.0;                   // y is the probability distribution function. Min y is 0.       Max y is 1.
	}                                 // y is a triangular distribution. Hence, y = (1/53)*x.
	while (1.0*rand()/RAND_MAX > y);  // Because we want to start with the WHYYYYYYYYYYY????????????
	return x;
}

Neither do I, really. I don't see how a random number should be existing in this loop, and then an X value to be returned. I can understand if you want to compare X > Y and change the while expression to

while (x / 53.0 > y);

so that a more valid x is returned when compared to a y value.


(With the original while loop). Imagine if the first loop, x was 50 and the random expression in the while (1 > .6), the loop would go again. Say by happenstance, x was 50 (again) and the random expression in the while loop was now (.6 > 1). The statement would turn false and now you would return x with value 50. To me this seems like a logical error. Granted, I don't know much more of what is going on other than the code that was given.

If the while loop was changed to

while (x / 53.0 > y);

x = 50.0 and then in while statement you'd get (50.0/53 > y), I can then understand why you would return the x value from the expression of the y value.

You may also want to check out this for Probability Distribution on wiki.

http://en.wikipedia.org/wiki/Cumulative_distribution_function


Finally, I could easily be wrong right from the get-go. I have never taken statistics but would be interested in seeing the answer.

Edited 5 Years Ago by Saith: n/a

The code does not make any sense. Would be helpful to describe what you are trying to do in words. For example what are you trying to do with the while loop? What is the problem you're trying to solve?

This article has been dead for over six months. Start a new discussion instead.