Asked by matlabkid602
on 23 Feb 2018

Say I have a uniform random distribution in matlab using the rand() function.

How can I change the distribution for a given function such as 1/sqrt(x), so the distribution follows this curve?

I've tried to reject values but had no luck.

Answer by Roger Stafford
on 24 Feb 2018

If you want to obtain a density distribution proportional to 1/sqrt(x) for x in some finite interval [a,b], you can proceed as follows. The cumulative probability function must be:

cdf(x) = (sqrt(x)-sqrt(a))/(sqrt(b)-sqrt(a))

which you get by integrating 1/sqrt(x) from a to x and adjusting the proportionality constant to get a cdf(b)=1 for the entire interval from a to b.

To generate this using rand, set the above cdf(x) to r = rand and solve for x:

r = rand;

%Solve for x in (sqrt(x)-sqrt(a))/(sqrt(b)-sqrt(a)) = r:

x = (sqrt(a)+(sqrt(b)-sqrt(a))*r).^2;

This illustrates how one would proceed to use rand for generating any given probability density function. It depends on being able to solve for x in an equation cdf(x) = r.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## John BG (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/384445-custom-uniform-random-distribution#comment_538761

Sign in to comment.