random integer between 0 and 15 with equal probability

조회 수: 8 (최근 30일)
Syed
Syed 2013년 7월 15일
댓글: ma ag 2022년 5월 24일
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일
B=randi([0,15])
  댓글 수: 7
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일
B1= ceil(b + (B+1-b) * rand)-1

Jan
Jan 2013년 7월 15일
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))

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by