# How do you generate a random number with a pre-specified probability distribution?

I want to generate a random real number between 0 and 1 but not distributed uniformly but with a probability distribution say, 2 Sin[ pi x ]^2 which integrates to 1 and peaks at $$x = 1/2$$.

I tried looking for Biased random variables in the HELP tab, but it did not show any hits.

This is my PDF:

pdf = 2 Sin[Pi x]^2

• Have you tried a RandomVariate of a ProbabilityDistribution with your chosen PDF function? Apr 25, 2022 at 11:32
• Please give actual code .. Apr 25, 2022 at 11:34
• This is a low-quality question. In the future, please make an effort to show due diligence. Provide a minimum working example of the code you have, tell us what have you tried, how did you search, and why the results were not satisfactory. Apr 25, 2022 at 12:10
• It's hard to provide code when you cannot spot the command that does the trick in the help menu. I tried looking for Biased random variables in the HELP tab, it did not show any hits ... Apr 25, 2022 at 12:22
• @QuasarSupernova if I Google "Mathematica" How do you generate a random number with a pre-specified probability distribution? (i.e the title you wrote) the first hit brings me to Random Number Generation, which contain in the second paragraph already an explicit mention of RandomVariate, which could have been a good start. Apr 25, 2022 at 12:48

dist = ProbabilityDistribution[2 Sin[Pi x]^2, {x, 0, 1}];

SeedRandom
sample = RandomVariate[dist, 500];

Show[Histogram[sample, 20, "PDF"], Plot[PDF[dist, x], {x, 0, 1}]] • If I am not mistaken, PearsonChiSquareTest[sample, dist, "TestDataTable"] results in $$\begin{array}{l|ll} \text{} & \text{Statistic} & \text{P-Value} \\ \hline \text{Pearson }\chi ^2 & 20.6 & 0.662195 \\ \end{array}$$ and does not confirm the accordance between sample and dist. Apr 25, 2022 at 13:20
• @user64494 Try comparing the P-value obtained using dist as the test distribution with that obtained using another test distribution on the same data (e.g. NormalDistribution[] or UniformDistribution[]) and see how the latter attempts result in much, much lower P-values. Apr 25, 2022 at 13:39
• @user64494 No. I suggest that the P-value you obtained is actually compatible with reasonable fit between the data and the proposed distribution. As a counterexample, I recommended running PearsonChiSquareTest[sample, UniformDistribution[], "TestDataTable"] to see an example of the kind of P-value that would be associated with a poor fit between data and test distribution. Apr 25, 2022 at 14:14
• @user64494, check PearsonChiSquareTest[sample, dist, "TestConclusion"]
– kglr
Apr 25, 2022 at 14:23
• 500 samples is not enough to reject any similar distribution, try dist2=NormalDistribution[Mean[dist],StandardDeviation[dist] ], PearsonChiSquareTest[smallsample, dist2, "ShortTestConclusion"] gives "Do not reject". You need around 5000 samples to reject a NormalDistribution of the same Mean and StandardDeviation. Apr 25, 2022 at 15:20

Since your PDF already integrates to one over the range this is pretty straight-forward:

RandomVariate[ProbabilityDistribution[2 Sin[Pi x]^2, {x, 0, 1}]]

(* 0.163801 *)