random integer between 0 and 15 with equal probability

4 Ansichten (letzte 30 Tage)
Syed
Syed am 15 Jul. 2013
Kommentiert: ma ag am 24 Mai 2022
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?

Akzeptierte Antwort

Matt J
Matt J am 15 Jul. 2013
Bearbeitet: Matt J am 15 Jul. 2013
B=randi([0,15])
  7 Kommentare
Stephen23
Stephen23 am 31 Aug. 2015
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 am 24 Mai 2022
lmao its ok to ask questions

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 15 Jul. 2013
B1= ceil(b + (B+1-b) * rand)-1

Jan
Jan am 15 Jul. 2013
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))

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by