random integer between 0 and 15 with equal probability

Hi,
I am trying to generate a random integer between 0 and 15.
I have written the following code:
b=0;
B=15;
B = b + (B-b) * rand
Is it right?
Also, will the random integer generated from the above code have equal probability? Is not, how should the code be modified?

 採用された回答

Matt J
Matt J 2013 年 7 月 15 日
編集済み: Matt J 2013 年 7 月 15 日

1 投票

B=randi([0,15])

7 件のコメント

Syed
Syed 2013 年 7 月 15 日
thanks Matt J
Syed
Syed 2013 年 7 月 15 日
one more thing: what if i want to avoid 0 being generated as an integer because i am getting infinity if there is a division process afterwards in my code?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 15 日
B=randi([1,15])
Matt J
Matt J 2013 年 7 月 15 日
Or,
B=randi(15);
Raziur Rahman
Raziur Rahman 2015 年 8 月 31 日
what about equal probability?
Stephen23
Stephen23 2015 年 8 月 31 日
When you read the randi documentation, its description states on the very first line "Uniformly distributed pseudorandom integers". This means the integers have equal probability. It also means that you should learn to read the documentation, because it tells you how MATLAB works.
ma ag
ma ag 2022 年 5 月 24 日
lmao its ok to ask questions

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

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 15 日

1 投票

B1= ceil(b + (B+1-b) * rand)-1
Jan
Jan 2013 年 7 月 15 日

0 投票

This is not a hard proof, but a strong hint already:
b = 0;
B = 15;
R = b + (B-b) * rand(1, 1e6);
histc(R, unique(R))

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

質問済み:

2013 年 7 月 15 日

コメント済み:

2022 年 5 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by