generate a random number base on pdf function
Show older comments
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.
thanks for your help.
3 Comments
John D'Errico
on 25 Jun 2022
Edited: John D'Errico
on 25 Jun 2022
Stop posting the same homework question miltiple times.
Note that having answered your question, IF you make an effort, (perhaps show what you tried as a comment on my answer) I might be willing to help you more if I see it as a serious effort.
hong chenyao
on 25 Jun 2022
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.
Accepted Answer
More Answers (3)
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 Comments
John D'Errico
on 25 Jun 2022
Edited: John D'Errico
on 25 Jun 2022
NO NO NO. You told the @hong chenyao how to generate a UNIFORM random number on a different interval. That was not remotely the question asked.
Karan Kannoujiya
on 25 Jun 2022
Ohh sorry, thanks for clearing
Image Analyst
on 25 Jun 2022
0 votes
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
0 votes
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
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

