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?

 Respuesta aceptada

Matt J
Matt J el 15 de Jul. de 2013
Editada: Matt J el 15 de Jul. de 2013
B=randi([0,15])

7 comentarios

Syed
Syed el 15 de Jul. de 2013
thanks Matt J
Syed
Syed el 15 de Jul. de 2013
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?
Or,
B=randi(15);
what about equal probability?
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 el 24 de Mayo de 2022
lmao its ok to ask questions

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 15 de Jul. de 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))

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 15 de Jul. de 2013

Comentada:

el 24 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by