Borrar filtros
Borrar filtros

getting a vector with random numbers but with new criteria

3 visualizaciones (últimos 30 días)
itay
itay el 9 de En. de 2015
Comentada: itay el 9 de En. de 2015
i need to get an 80 cells vector with random numbers that will be between 1 to 8.
each number, x for example, need to be different from x+1 and x-1, and also different from x+2 and x-2.
to make it clear:
what i need is like: 4-1-5-3-2-6-4-2-3-5-...
and what i have now is: 3-5-1-5-7-4-5-3-4-3-...
is it possible in matlab?
thanks.
  2 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 9 de En. de 2015
There is no a general solution. You have to precise what you want
itay
itay el 9 de En. de 2015
what do you mean not general?
i need "n" random numbers that are betwwen the range "k to z" that the numbers in places "x+1","x-1", and "x+2", "x-2" are different then the number that in place "x"..
that will help me on making a task where i can run few random pictures and every few pictures i have a repeat on the last one showed (like: a - b - c - d - d - a - e - e - f - g - e - a - a - d - ...)

Iniciar sesión para comentar.

Respuestas (1)

Roger Stafford
Roger Stafford el 9 de En. de 2015
Editada: Roger Stafford el 9 de En. de 2015
You want n random integers, each ranging from k to z, such that each differs from the two previous integers. Call the vector of integers V and do this:
V = zeros(1,n);
V(1) = randi([k,z]);
d = setdiff(k:z,V(1));
V(2) = d(randi(z-k));
for m = 3:n
d = setdiff(k:z,[V(m-2),V(m-1)]);
V(m) = d(randi(z-k-1));
end

Categorías

Más información sobre Random Number Generation 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