# generate a random number base on pdf function

17 views (last 30 days)
hong chenyao on 25 Jun 2022
Commented: hong chenyao on 5 Jul 2022
I basically have the following PDF respectivley:
f(x)=1/2*x+0.5
range is -1 to 1
I would like to generate random numbers on matlab based on these equations.
John D'Errico on 25 Jun 2022
Edited: John D'Errico on 25 Jun 2022
It does not matter if it is MATLAB homework, or homework for some other class. IT IS HOMEWORK. It is YOUR HOMEWORK. It was assigned to you. We are not a homework solving service. You have made no attempt to do your homework.
How about this as an option. Ask your teacher to contact me directly. Have them give me their direct agreement that it is OK if I do your homework assignments for you. I'll send the completed homework directly to your teacher. Of course, then I'll be the one who gets credit. Can I get yet another degree? Maybe. Do I want one? Nope.

John D'Errico on 25 Jun 2022
Edited: John D'Errico on 25 Jun 2022
First, is that the PDF of a random variable? If it was, the integral would be 1.
syms x
P_x = x/2 + 1/2;
int(P_x,-1,1)
ans =
1
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:
int(P_x,[-1,x])
ans = 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.
hong chenyao on 5 Jul 2022

Karan Kannoujiya on 25 Jun 2022
Hi hong,
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)
##### 2 CommentsShow 1 older commentHide 1 older comment
Karan Kannoujiya on 25 Jun 2022
Ohh sorry, thanks for clearing

Image Analyst on 25 Jun 2022
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 on 3 Jul 2022
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