getting a vector with random numbers but with new criteria

4 Ansichten (letzte 30 Tage)
itay
itay am 9 Jan. 2015
Kommentiert: itay am 9 Jan. 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 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 9 Jan. 2015
There is no a general solution. You have to precise what you want
itay
itay am 9 Jan. 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 - ...)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Roger Stafford
Roger Stafford am 9 Jan. 2015
Bearbeitet: Roger Stafford am 9 Jan. 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

Kategorien

Mehr zu Random Number Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by