Understanding the While Statement in a Do-While Loop

  • Thread starter spaghetti3451
  • Start date
  • Tags
    Loop
In summary, the while statement is used to keep looping while a condition is met. The condition is based on a probability distribution function. This probability distribution is generated by a random number generator. The random number generator is used to create a new probability distribution function. This new function is used to determine when the while statement should break off.
  • #1
spaghetti3451
1,344
33
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
  • #2
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.
=).
 
  • #3
The second random value has to be bigger than the first. How does that ensure that the distribution becomes triangular?
 
  • #4
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.
 
  • #5
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
 
  • #6
why loop?
r=min(rand(), rand());
 
  • #7
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).
 

1. What is a do-while loop?

A do-while loop is a type of control flow statement in programming that executes a block of code repeatedly until a certain condition is met. It is similar to a while loop, but the condition is evaluated after the code block is executed, guaranteeing that the code will be executed at least once.

2. How is the while statement used in a do-while loop?

The while statement in a do-while loop is used to evaluate the condition that determines whether the loop should continue to execute or stop. If the condition is true, the loop will continue to execute; if it is false, the loop will end.

3. What happens if the condition in a do-while loop is never true?

If the condition in a do-while loop is never true, the loop will continue to execute indefinitely, resulting in an infinite loop. This can cause the program to crash or become unresponsive. It is important to ensure that the condition in a do-while loop will eventually become true to avoid this issue.

4. Can you give an example of a do-while loop?

Yes, here is an example of a do-while loop in Java:

int i = 1;
do {
 System.out.println(i);
 i++;
} while (i <= 10);

This loop will print the numbers 1 to 10.

5. What are some common use cases for do-while loops?

Do-while loops are commonly used when you want to execute a block of code at least once, regardless of the condition, and then continue executing the code as long as the condition is true. They are often used for user input validation, menu systems, and error handling. They can also be useful in situations where the number of iterations is not known beforehand.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
7
Views
990
  • Programming and Computer Science
Replies
11
Views
1K
Back
Top