0

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

#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 help me to understand what this code is doing and how it could be used to create three components of a momentum magnitude. Thank you kindly.

daw531
  • 1
  • use a debbugger or print x, y to have more info – Gabriel Nov 12 '14 at 15:37
  • If you want to have random x-, y- and z-value of momentum use the magnitude, the polar- and the azimuthal angle (usually denoted with theta and phi). Create all of them randomly and then compute x, y and z accordingly -> http://en.wikipedia.org/wiki/Spherical_coordinate_system Alternatively you can directly generate x, y and z randomly preserving in each step the constraints (if any) like max. magnitude. – a_guest Nov 12 '14 at 15:43
  • Thank you, that's all very helpful but I am still really just looking for a good explanation of what the code I have posted is doing. Is it just creating a random number? The context of the question I have been set to answer gives the impression that this piece of code will entirely generate a magnitude and the three components of that magnitude. It doesn't look to me like that is what it is doing but I could well be wrong. If that is not what it is doing then I am to presumably modify this code or use it repeatedly to acheieve the generation of the magnitude and components. – daw531 Nov 12 '14 at 16:10
  • For example I think the line `x=momentum*rand()/RAND_MAX` will generate a number between 0 and momentum then the line `y=x/momentum` sets y equal to the ratio of the random number with momentum but I don't really understand what the while condition is saying. – daw531 Nov 12 '14 at 16:35
  • This code generates as long random numbers between `0` and `momentum` as the following call of `rand()` is smaller than the previous. In case it's equal or less the function returns the last generated value between `0` and `momentum`. This gives you the random value for one component. If you want to compute all three components you have to call it three times for x, y and z. If you have the constraint that the **absolute value** of the momentum vector has to be less than e.g. `pmax` don't forgot to call the function the 2nd time with `sqrt(pmax*pmax-px*px)` and the third time also accordingly. – a_guest Nov 12 '14 at 18:22

0 Answers0