I need to generate 500 numbers from a distribution specified. I used the well-known CDF method, but since I'm new to Mathematica, I don't know how to set a condition on the sum of these variables.
dist :=
ProbabilityDistribution[(2/Sqrt[Pi]* Exp[-1/x])/x^(5/2), {x, 0, ∞}];
g[x_] = CDF[dist, x];
h =
Table[{g[x], x}, {x, 0, 5500}] // N // Interpolation[#, InterpolationOrder -> 5] &;
d = 1;
While[d < 5990 || d > 6010,
h /@ (RandomReal[{0, 1}, {500}]);
d = Total[h]]
Where, with Total
, I'm trying to obtain the sum of the interpolated array but for some reason it gives me this error:
Lists of unequal length in InterpolatingFunction...
Update:
d = 1; While[d < 5990 || d > 6010,
a = h /@ (RandomReal[{0, 1}, {500}]);
d = Total[a]]
This works. Tahnks anyway for the help.
SeedRandom[1234]; data = RandomVariate[dist, 500];
TheSeedRandom
is needed to give you reproducible results. $\endgroup$2/Sqrt[π t] Exp[-1/t] + Erfc[1/Sqrt[t]]
. You can perhaps try using it inFindRoot[]
to implement the inverse CDF. Alternatively, you could look into usingNDSolve[]
, as was done here. $\endgroup$