getting a vector with random numbers but with new criteria

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Azzi Abdelmalek
Azzi Abdelmalek il 9 Gen 2015
There is no a general solution. You have to precise what you want
itay
itay il 9 Gen 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 - ...)

Accedi per commentare.

Risposte (1)

Roger Stafford
Roger Stafford il 9 Gen 2015
Modificato: Roger Stafford il 9 Gen 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

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by