# Generating random numbers from given continuous distribution

Posted 10 months ago
1123 Views
|
5 Replies
|
5 Total Likes
|
 Hi, I have a given probability density function, which is 0.001257 *x^4 Exp[-0.285714 *x] and I need to generate 15 random numbers according to this distribution. Here is what I have done: dist = ProbabilityDistribution[ 0.001257*x^4 Exp[-0.285714*x], {x, 0, 60}] Plot[PDF[dist, x], {x, 0, 60}] sample = RandomVariate[dist, 15] {5.75881, 4.74259, 6.97658, 5.52833, 7.34263, 6.76362, 4.92664, 2.58222, 4.65696, 6.4487, 7.30355, 7.25276, 7.00075, 4.12756, 4.41973} Show[Histogram[sample, 100, "ProbabilityDensity"], Plot[PDF[dist, x], {x, 0, 60}, PlotStyle -> PointSize[Medium]]] I think this is pretty straightforward, and I followed several examples that aim at doing the same, but my results are weird. I get a sequence of 15 numbers between 0 and 8, and I don't understand why. All in all, I think I am not getting right how to generate random variates from a custom distribution in the first place. Would you help me?
5 Replies
Sort By:
Posted 10 months ago
 Hi Elena,You need to normalize the distribution so its integral is 1.  dist = ProbabilityDistribution[ 0.001257*x^4 Exp[-0.285714*x], {x, 0, 60}, Method -> "Normalize"] RandomVariate[dist, 15] (* {27.4943,16.8757,5.30406,6.00207,11.8668,28.2957,21.9066,19.6846,12.266,18.8735,19.3128, 15.404,21.1927,13.8153,19.9035} *) 
Posted 10 months ago
 Thanks a lot. Just a question, does this work on Mathematica 10? Because I can make it work on the web version of Mathematica, but not on my laptop, where I have v10.
Posted 10 months ago
 For what I can see the option Method->Normalize was introduced in version 10.2
 For a distribution of this form you can compute the normalization factor. factor = NIntegrate[0.001257*x^4 Exp[-0.285714*x], {x, 0, Infinity}]; dist = ProbabilityDistribution[ 0.001257*x^4 Exp[-0.285714*x] / factor , {x, 0, 60}]