About Invariant mass

I have defined a poisson distribution function and create random values for px, py, pz(components of momentum) using this function. Also i defined mass using m=gRandom->Uniform(0,2000) and similarly energy e=gRandom->Uniform(0,2000). Then i calculate energy using E=sqrt(pxpx+pypy+pzpz+mm).
Now I define two Lorentz vector. In one Lorentz vector, I use px, py, pz and e(randomly generated energy) and then calculate invariant mass. In second lorentz vector, i use px, py, pz and E(energy using formula) and then again calculate invariant mass. Both Invariant masses are different. Why is it so??
Should I define mass and energy(that i have chosen randomly) in some other way or something else??

You are trying to correlate two random generated numbers. It is obvious that the 2 invariant masses are different.
Morever using a random generated energy to compute an invariant mass most of the times will result in an imaginary mass if e < | p|

So is there any way to define random numbers for energy and mass so that i can compare the two mass values obtained at the end using Lorentz vector.

What are you asking is that
\sqrt{e^2 -p_x^2 - p_y^2 - p_z^2} \simeq m
where you randomly generate both e and m
There is no way to do what you are asking for.

lv_mass.C (2.4 KB)
Here you can see the code.
I want to compare mass and mass_1(see in code) and I want them to be comparable. What should I do??

There is nothing you can do, there is no physical and logical reason why mass_1 shoul dbe equal to mass.

mass will be always equal to the m you generate randomly.

But energy e is totally unrelated, there is no way that a random number from uniform range (0,2000) should be similar or related to another number from an uniform distribution (0,2000) on which you perform several operations.

Maybe you did not understand properly the task that was assigned to you.