フィルターのクリア

generating random variable between 0 and 1 with probabilities

4 ビュー (過去 30 日間)
william Smith
william Smith 2019 年 3 月 10 日
コメント済み: william Smith 2019 年 3 月 10 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 3 月 10 日
If the percent_shift is 0.4 then you total 110% probability.
william Smith
william Smith 2019 年 3 月 10 日
sorry typo erro

サインインしてコメントする。

採用された回答

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
william Smith
william Smith 2019 年 3 月 10 日
Awsesome!
Thank you so much!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by