triangular distribution for creating momentum four vector

Hello.

In my problem I am to create a base class to represent a four vector (a concept in physics involving a four dimensional vector) then create a derived class specifically to represent the four momentum of a particle which inherits from the base class. I have been supplied a small piece of code to use to generate a 'random' x y and z component of the momentum magnitude. The code is as follows

1
2
3
4
5
6
7
8
9
#include <cstdlib>
double triangular(double momentum){
	double x, y;
	do{
		x = momentum*rand()/RAND_MAX;
		y = x/momentum;
	} while (1.0*rand()/RAND_MAX > y);
	return x;
}


It is said in my problem that this code is supposed to generate the magnitude, and then randomly split into x, y and z components. This code returns a single value and so I cannot see how it is doing what it says in the problem. Could someone explain this code to me please. Thank you kindly.
please,, I need help,,,

its C++ question, Please

You need to write a code, which will calculates results, depending on the values that user enters to the running software: i.e.
1) Y=1/A i.e Y – is result, A is a variable, and it is known, that the variable A has values from the interval: A_begin, A_end, and the step (A_step) which describes the step (speed) in which variables varies. Result should be printed out in „table“ that it has columns, and we can easy recognize, which column has values of Y, and which column has values of A, etc.

Let say; we have several examples: A_begining = 1, A_end = 4, A_step = 1, that the software should make calculations on several A values (Y=1/1, Y=1/2, Y=1/3, Y=1/4) and print out the values of A and result of Y, every time it will make calculations.

If we have A_begining = 1, A_end = -4, A_step = -1, your software should make all posible calculations with A values (1, 0, -1, -2, -3, -4) , and print result on spcreen. You need to make software that it will not HANG, and run correclty on any A values, that user will enter to it. All examples that you need to pay attention when caculations can be done, and when not, what condition of A_begin vs A_end and A_step should be analyzed where explain in 116 room.



The result in table could be provided such way (or like this)



!-----------------!---------------!

! value of A ! result Y !

!-----------------!---------------!

! 1 ! 1.0 !

! 2 ! 0.5 !

------------------------------------



And so on....







The same is for others three calculations.

Y=B/SQRT(A), B and A – provided bu user

Y = C/(B-A), all values provided bu user, BUT:

Y=xN X and N provided by user
Umm...was this an answer to my question? I don't get it! I just want a qualitatve description of how the code I supplied actually works and how it is supposed to be used to generate a 3D momentum vector.

Thanks again.
Topic archived. No new replies allowed.