MATLAB: How to generate data based on uninvertible probability distribution function

MATLABrandom number generatorsimulation

I want to generate data based on given probability distribution.
f(y) = alpha*exp(-y/beta1)+(1-alpha)*exp(-y/beta2)
I tried to use the way as follows. I used inverse function and reversed a probability distribution function to get the inverse result.
syms y
f(y) = alpha*exp(-y/beta1)+(1-alpha)*exp(-y/beta2);
g = finverse(f);
This method doesn't work since "Functional inverse cannot be found. " However, I find another code written by a programmer. This code can generate the data series perfectly. But I cannot understand its thought. Could you help me explain it?
m = [beta1;beta2]; % the two means
t = 1 + (randn(n,1)<alpha); % randomly select 1st or 2nd mean
X = m(t) .* -log(rand(n,1)); % generate exponentials with selected mean

Best Answer

a. If your f(y) is meant to be the cumulative probability distribution over a y support from zero to infinity, it is backwards from the usual convention. For y = 0 it should be zero and at infinity it should be one. To get that, subtract your expression from 1. (It certainly isn't a density distribution.)
b. I have serious doubts that the code you describe will give the desired distribution. I think you need to use matlab's 'fzero' function for taking the inverse combined with the use of 'rand' to obtain what you want.