How to generate random integers from the uniform distribution between two numbers

6 visualizaciones (últimos 30 días)
Assume matrix A as follows:
A = [
810 840
840 870
870 900
900 930
930 960
];
For every row in matrix A,I want to generate random integers from the uniform distribution between column 1 and column 2. Also, these random numbers should be divided by 5 with decimal 0 (e.g. 810 (not 811)).
The output matrix size should be 5*1.
I think the right function is randi, but I don't know how to form it.
  3 comentarios
Zhan
Zhan el 21 de Mayo de 2017
Thank @the cyclist.
Yes, the entire in A always multiples of 10. For the second part of your question, also, Yes. For example for the first row, 810 and/or 40 can be included. Thanks!
Stephen23
Stephen23 el 22 de Mayo de 2017
@Zhan: you keep asking questions, but you have not accepted any of the answers to any of your questions. Accepting answers shows which answer solved your question, and is also an easy way for you to say "thank you" for our volunteer effort helping you.

Iniciar sesión para comentar.

Respuesta aceptada

the cyclist
the cyclist el 21 de Mayo de 2017
Editada: the cyclist el 21 de Mayo de 2017
Under the assumption that you want to include the endpoints, and that the elements of A are evenly divisible by 5:
A = [
810 840
840 870
870 900
900 930
930 960
];
nrows = size(A,1);
r = zeros(nrows,1);
for nr = 1:nrows
r(nr) = 5*randi(A(nr,:)/5);
end

Más respuestas (1)

Guillaume
Guillaume el 21 de Mayo de 2017
Simply loop over the rows:
result = zeros(size(A, 1), 1);
for row = 1:size(A, 1)
result(row) = randi(A(row));
end
I do not understand "these random numbers should be divided by 5 with decimal 0"
  3 comentarios
Guillaume
Guillaume el 21 de Mayo de 2017
Oh! Well, in that case, simply divide the bounds by 5 and multiply the integer results by 5 afterward.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by