generate a random number base on pdf function
6 ビュー (過去 30 日間)
John D'Errico 2022 年 6 月 25 日
編集済み: John D'Errico 2022 年 6 月 25 日
First, is that the PDF of a random variable? If it was, the integral would be 1.
P_x = x/2 + 1/2;
And of course, P_x is always positive on that domain. So indeed, this has the necessary properties of a PDF.
Now, assuming this is not homework... Sadly, I wonder if it is homework. This has all the hallmarks of a homework probem. You are a new user, who has never asked a question here before. This is a fairly basic question, and the given PDF is such a nicely posed one. Essentially, it is too basic a question, with a perfectly posed question. Yep, I'd bet a lot this is just homework, with no effort shown.
Oh well, having started to write this, and since it MAY possibly not be homework, here is what you do:
First compute the CDF. That is just the integral of P_x, represented as a function of x. Hint:
I would test it. Does that have the desired properties as a CDF?
Next, you generate a random number in the interval [0,1]. Call it r.
Finally, compute the inverse of the CDF of the value r, thus solve for x, such that CDF(x) == r.
x as generated will have the desired triangular distribution.
You need to do the rest.
その他の回答 (3 件)
Karan Kannoujiya 2022 年 6 月 25 日
so your range is between -1 to 1 in that case your domain will be between -3 to 1
if you want to generate N random number between two number 'a' and 'b' then you can use the below syntax
In your case a=-3 and b=1
r = a + (b-a) .* rand(N,1)
Image Analyst 2022 年 6 月 25 日
You need to do "inverse transform sampling" so you need the CDF, as the esteemed @John D'Errico said.
I'm attaching an example I worked up for drawing samples from a Rayleigh distribution. Adapt as needed.
Shivam Lahoti 2022 年 7 月 3 日
Tons of distributions are given here: http://www.mathworks.com/matlabcentral/fileexchange/7309-randraw
In general, you basically compute the CDF of your PDF function and invert it. Go here for a generally applicable explanation of how to do it: http://en.wikipedia.org/wiki/Inverse_transform_sampling