generating random variable between 0 and 1 with probabilities

Hi all,
I have two questions please:
1) how do i generate a random number either 0 or 1 with equal chances of accept and reject 50-50 chance?
2) how do i generate a random number either 0 or 1 with percent shift in probablity in order to accept "bias' between -0.5 and 0.5
if percent shift=0 then 50-50 chance of accept
if percent shift=0.1 then 60% accept - 40% reject
if percent shift=0.2 then 70% accept - 30% reject
if percent shift=0.3 then 80% accept - 20% reject
if percent shift=0.4 then 90% accept - 20% reject
if percent shift=0.5 then 100% accept - 0% reject
if percent shift=-0.1 then 40% accept - 60% reject
if percent shift=-0.2 then 30% accept - 70% reject
if percent shift=-0.3 then 20% accept - 80% reject
if percent shiftt=-0.4 then 10% accept - 90% reject
if percent shift=-0.5 then 0% accept - 100% reject
Thanks!

2 Commenti

If the percent_shift is 0.4 then you total 110% probability.

Accedi per commentare.

 Risposta accettata

rand() < (percent_shift + 0.5)

5 Commenti

%Thank you!
%will this be the way to put it in a function:
i=1
function [choice_1] = customer_bias(customer_1,bias)
if bias(i)=0 %then 50-50 chance of accept
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=0.1 %60 accept - 40% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=0.2 %70% accept - 30% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=0.3 then %80% accept - 20% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=0.4 then %90% accept - 10% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=0.5 then %100% accept - 0% reject
rand(choice_1) < (bias(i) + 0.5)
elseif percent shift=-0.1 then %40% accept - 60% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=-0.2 then %30% accept - 70% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=-0.3 then %20% accept - 80% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=-0.4 then %10% accept - 90% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=-0.5 then %0% accept - 100% reject
rand(choice_1) < (bias(i) + 0.5)
i=i+1
end
%Thanks again
In MATLAB, = is only for assignment, not for comparison.
Also, what if the user passes something that is not bit-for-bit identical to how MATLAB convers the literal 0.3 (for example) ? Suppose they pass in 0.25 for the bias?
%would this make a better sense?
i=1
function [choice_1] = customer_bias(customer_1,bias)
if bias(l)>=0 && bias(l)<0.1 %then 50-50 chance of accept
rand(choice_1) < (bias(i) + 0.5)
elseif bias(l)>= 0.1 && bias(l)<0.2 %60 accept - 40% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(l)>=0.2 && bias(l)<0.3 %70% accept - 30% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(l)>=0.3 && bias(l)<0.4 then %80% accept - 20% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(l)>=0.4 && bias(l)<0.5 %90% accept - 10% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=0.5 then %100% accept - 0% reject
choice_=1;
elseif bias(l)>=-0.1 && bias(l)<-0.2 %40% accept - 60% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(l)>=-0.2 && bias(l)<-0.3 %30% accept - 70% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(l)>=-0.3 && bias(l)<-0.4 %20% accept - 80% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(l)>=-0.4 && bias(l)<-0.5%10% accept - 90% reject
rand(choice_1) < (bias(i) + 0.5)
elseif bias(i)=-0.5 then %0% accept - 100% reject
choice_1=0
i=i+1
end
The assignment to i before the function definition is in a different workspace than the function uses. Inside your function, both i (lower-case I) and l (lower-case L) are undefined. You also switch back and forth between using i or l as the subscript.
You assign a value to choice_ in the case of bias 0.5 but you never use that variable.
You only assign a value to the output variable choice_1 in the case that the input bias is -0.5
You try to test 0.5 and -0.5 for equality using = as the comparison operator, but = is only ever assignment (except some cases having to do with older versions of the symbolic toolbox.) == is the comparison test.
You input customer_1 but you never use it.
You use choice_1 to pass a size argument to rand() but you have not defined choice_1 by that point.
It is not clear what your intended output is. Is it the random variable such as 0.013433807 ? Is it 0.0 or 0.1 (double precision numbers) ? Is it uint8(0) or uint8(1) ? Is it false or true (logical values) ?
Why are you indexing bias? Are you expecting it to be a vector of values?
Each of your cases appears to have the same action except for 0.5 and -0.5 . Why not just test those two specifically, like
if bias(i) == 0.5
choice_ = 1;
elseif bias(i) == -0.5
choice_1 = 0;
else
rand(choice_1) < (bias(i) + 0.5)
end
Awsesome!
Thank you so much!!

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by