1

I know the probability density function (PDF) expression of random variable r as 2r/R^2 where 0<=r<=R. Then, its CDF is r^2/R^2.

Can someone help me to generate random variable r in MATLAB following above distribution?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Frey
  • 315
  • 4
  • 11
  • Possible duplicate of [Generate random numbers according to distributions](https://stackoverflow.com/questions/3510475/generate-random-numbers-according-to-distributions) – Gelliant Jun 04 '18 at 08:30
  • 2
    I think `sz = [1 1e6]; R = 5; x = sqrt(rand(sz))*R; histogram(x)` does what you want – Luis Mendo Jun 04 '18 at 08:42

1 Answers1

1

https://blogs.sas.com/content/iml/2013/07/22/the-inverse-cdf-method.html

I use the same variables as they use

f(x) = 2x/R^2

F(x) = x^2/R^2

solving for x in the equation F(x) = u

u*R^2 = x^2

x = sqrt(u * R^2) v -sqrt(u * R^2)

in Matlab:

N=1E5;
R=1;
u = rand(1,N);
x = sqrt(u*R^2); 

histogram(x)
Gelliant
  • 1,835
  • 1
  • 11
  • 23
  • Your distribution is not entirely correct as he has defined it from 0 to R, (thus the 2 in the PDF). So the negative part of the squareroot should be "ignored". – Nicky Mattsson Jun 04 '18 at 09:15
  • You are welcome - but please remove the incorrect code rather than commenting it out. – Nicky Mattsson Jun 05 '18 at 13:57
  • More on the Inverse Transform at https://stats.stackexchange.com/questions/184325/how-does-the-inverse-transform-method-work and [here](https://en.wikipedia.org/wiki/Inverse_transform_sampling). – SecretAgentMan Nov 10 '18 at 04:07