# RandomVariate of a custom distribution gives only negative points

I don't understand why the following code

dis = ProbabilityDistribution[1/(x^2 + 1), {x, -5, 5}];
data = RandomVariate[dis, 10^4];;
Show[
Histogram[data, 20, "ProbabilityDensity"],
Plot[PDF[dis, x], {x, -5, 5}, PlotStyle -> Thick],
PlotRange -> {{-5, 5}, {0, 1}}]


Produces only negative data points? At the same time for the dis = NormalDistribution it works fine.

• It would probably be more useful if you self-answer. The question's code is simple, and I bet many people would have this problem too. Self-answering is a practice that is encouraged on this site. Sep 20, 2015 at 16:06

I understood it. The argument of the ProbabilityDistribution should be properly normalized. It would be actually useful if mathematica produced some kind of error message in this example. So the correct code should be:

norm = NIntegrate[1/(x^2 + 1), {x, -5, 5}];
dis = ProbabilityDistribution[(1/(x^2 + 1))/norm, {x, -5, 5}];
data = RandomVariate[dis, 10^4];
Show[
Histogram[data, 20, "ProbabilityDensity"],
Plot[PDF[dis, x], {x, -5, 5}, PlotStyle -> Thick],
PlotRange -> {{-5, 5}, {0, 0.4}}]
Export["c:/d/dist.png", %];


• Note that in 10.2 and later you can do ProbabilityDistribution[1/(x^2 + 1), {x, -5, 5}, Method -> "Normalize"] and Mathematica will take care of the normalization for you. Sep 21, 2015 at 19:23