Understanding the While Statement in a Do-While Loop

  • Thread starter Thread starter spaghetti3451
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around understanding the role of the while statement in a do-while loop, particularly in the context of generating a triangular distribution from a uniform distribution using random values. Participants explore the mechanics of the algorithm and its implications for probability distributions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants express confusion about the necessity of the while statement in the do-while loop.
  • One participant explains that the while loop is used to repeat the random generation process until a certain condition is met, which is intended to create a triangular distribution.
  • Another participant describes how the second random value must be greater than the first to ensure that the distribution becomes triangular, providing a hypothetical example with a simplified RAND_MAX value.
  • Further, a participant illustrates the concept using a comparison of random numbers, arguing that a higher first number increases the likelihood of acceptance in the distribution.
  • Some participants propose alternative methods to achieve a triangular distribution, such as using the minimum or maximum of two random values, suggesting these methods could optimize the algorithm.
  • There is mention of the rejection-sampling algorithm as a class of methods related to the discussed approach.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the necessity of the while statement, with some supporting its use while others suggest alternative methods. The discussion remains unresolved regarding the optimal approach to generating a triangular distribution.

Contextual Notes

Participants rely on specific assumptions about the behavior of random number generation and the properties of distributions without fully resolving the implications of these assumptions. The discussion includes varying interpretations of how the triangular distribution is achieved.

spaghetti3451
Messages
1,311
Reaction score
31
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;
}
 
Technology news on Phys.org
failexam said:
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;
}

rand() yields a uniform distribution.
As it says in the comment, y is supposed to be a triangular distribution.
By repeating randomly, based on a second random value, the distribution becomes triangular.
The chance on 0.0 will be 1 in RAND_MAX * (RAND_MAX + 1) / 2.
The chance on 1.0 will be 1 in (RAND_MAX + 1) / 2.

There is a simular algorithm to convert the uniform distribution into a normal distribution.
=).
 
The second random value has to be bigger than the first. How does that ensure that the distribution becomes triangular?
 
failexam said:
The second random value has to be bigger than the first. How does that ensure that the distribution becomes triangular?

Let z be the second random value.
Suppose RAND_MAX = 3.
Then we would have the following table:
Code:
  y      0     1/3     2/3     3/3      
z
0/3      0     1/3     2/3     3/3
1/3   reroll   1/3     2/3     3/3
2/3   reroll  reroll   2/3     3/3
3/3   reroll  reroll  reroll   3/3
If y has a value in the right upper triangle, the while-loop breaks off and we have our result.
If y has a value in the lower right triangle, the while-loop is repeated, and both y and z are rolled again.

Effectively our new distribution is a triangular distribution, in which the chance on y=1.0 is greater than the chance on y=0.0.
 
failexam said:
The second random value has to be bigger than the first. How does that ensure that the distribution becomes triangular?

If I pick a random number 1-100, and you pick a random number 1-100, and mine has to be bigger than yours in order to work, what happens?

If I pick the number 8, that's pretty low. So there's a good chance that the number YOU pick will be higher, and my number 8 will be rejected.

However, if I pick 97, that's really high. The chances that you pick a random number that's HIGHER than 97 are pretty low, so chances are good that my number 97 will get accepted.

It's not a guarantee-- but it's likely.

DaveE
 
why loop?
r=min(rand(), rand());
 
Xitami said:
why loop?
r=min(rand(), rand());

Very good!
That would work as well to get a triangular distribution.
I would use r=max(rand(), rand()); though to get the same distribution.

The corresponding table is:
Code:
  y     0     1/3     2/3     3/3      
z
0/3     0     1/3     2/3     3/3
1/3    1/3    1/3     2/3     3/3
2/3    2/3    2/3     2/3     3/3
3/3    3/3    3/3     3/3     3/3

In the first case we had chances 1/10, 2/10, 3/10, and 4/10.
In this case we have chances 1/16, 3/16, 5/16, 7/16.
Both are proper triangular distributions.

This would be an optimization of the algorithm.
The current algorithm is based on the class of rejection-sampling algorithms (http://en.wikipedia.org/wiki/Rejection_sampling).
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
22
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K