We can generate random numbers in an interval [a,b] easily if we want to make it uniformely:
A=rand()*(b-a) + a
where rand()
is a function which can generate a uniform random number between 0 and 1. so A
is a random number in [a,b]
.
For generating a random number based on a distribution function like y=x-x^2
, i faced a problem.
I would like to use a method mentioned here. But i am not interested to use the python function inverse_cdf(np.random.uniform())
.
I can compute the CDF of function "y" by an integration over 0 and X and i call it "f". But when i put the rand() function(a number between 0 and 1) into the inverse function of f, i get a complex number!
It means: A=f^(-1) (rand())
returns a complex number.
Is it a correct way for generating a random number based on a distribution function?
I used this website to compute the inverse of f=x^2/2 - x^3/3
and the code below is a part of the calculation that shows that the tmp1
is always negative
for i=1:10
rnd1=rand;
tmp1 = 2*sqrt(6)*sqrt(6*rnd1^2-rnd1)-12*rnd1+1
cTmp1 = tmp1^(1/3)
end