generating random variable between 0 and 1 with probabilities

4 次查看(过去 30 天)
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!

采纳的回答

Walter Roberson
Walter Roberson 2019-3-10
rand() < (percent_shift + 0.5)
  5 个评论
Walter Roberson
Walter Roberson 2019-3-10
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by