Randomize vector avoiding consecutive values
13 views (last 30 days)
Show older comments
I have this vector "leftSideProbes_random" and I would like to shuffle the values so that in the final vector the values do not appear consecutively (e.g., two same values do not appear in succession).
leftSides = [1,3,5];
leftPos_trials = repmat(leftSides,1,14);
leftSideProbes_random = randsample(leftPos_trials,42);
leftSideProbes=leftSideProbes_random(randperm(42));
I've tried randperm and other different alternatives, but none of them seem to work.
Any help would be very much appreciated.
Thanks,
Mikel
1 Comment
Torsten
on 2 Feb 2023
Edited: Torsten
on 2 Feb 2023
I have this vector "leftSideProbes_random" and I would like to shuffle the values so that in the final vector the value does not appear consecutively.
Depending on what "randsample" returns, this will not be possible in all cases.
Further, if you want that in the final vector two same values do not appear consecutively, it's no longer random.
Accepted Answer
Voss
on 2 Feb 2023
Here's a method that picks values randomly, one at a time, from a set that excludes the most recently picked value
leftSides = [1,3,5];
n_reps = 14;
n = numel(leftSides);
all_idx = 1:n;
result = zeros(1,n*n_reps);
result(1) = randi(n);
available = true(1,n);
for ii = 2:n*n_reps
available(result(ii-1)) = false;
temp_idx = all_idx(available);
result(ii) = temp_idx(randi(n-1));
available(result(ii-1)) = true;
end
leftPos_trials = leftSides(result)
find(diff(leftPos_trials) == 0) % check for consecutive repeats
8 Comments
Voss
on 20 Feb 2023
If that gives you 104 elements, just remove the first or last element afterwards:
d(end) = []; % for example
More Answers (2)
David Hill
on 2 Feb 2023
leftSides = [1,3,5];
leftPos_trials = repmat(leftSides,1,14);
idx=randperm(42);
while any(diff(leftPos_trials(idx))==0)
idx=randperm(42);%try until you get one to work
end
leftPos_trials(idx)
John D'Errico
on 2 Feb 2023
Edited: John D'Errico
on 2 Feb 2023
Typically, rejection would be necessary. Of course, if your initial vector were composed only of two elements, then there are only two possible "random" configurations that lack any direct repeats, thus the sequence
01010101010101010101...
or
10101010101010101010...
In your case, it is not too much better. You want random sequences of 3 elements where no element appears consecutively. Again, rejection would seem to be the solution. But an alternative is not too difficult to build that does not involve rejection. The advantage is if you have a long sequence, rejection will often be almost impossible to use, since some repeats will almost always happen, and then rejection would just have you start over again.
Anyway, the function randwithoutreps will suffice. It does not use rejection at all. It could probably be written in a better way, but there is no real need for that. One should never spend time to improve efficient code.
randwithoutreps(5)
It is reasonably fast.
timeit(@() randwithoutreps(1000))
So a fraction of a second to generate a sequence as desired with length 3000 elements. If you tried to solve that using rejection, you would be waiting for years probably until you got lucky enough.
any(0==diff(randwithoutreps(10000)))
So a sequence of length 30000 elements. As you can see, there were no repeated elements.
hist(randwithoutreps(1000))
And each element appeared exactly 1000 times.
If you wanted some other elements, then just use the result as an index into another vector.
V = [2 3 5];
V(randwithoutreps(5))
The code is pretty simple.
function vec = randwithoutreps(reps)
% generate a random sequence of elements from the set [1 2 3] with no repeats
% usage: vec = randwithoutreps(reps)
% reps the number of times each element will appear
vec = zeros(1,3*reps);
% choose in advance where to locate the 1 elements first, so there are
% no repeated ones. This means we must ALWAYS have at least one other
% element between each pair of ones.
locs1 = 1:3*reps;
for i = 1:reps
% choose any element randomly from the vector locs
loc = randi(numel(locs1));
loc = locs1(loc);
vec(loc) = 1;
locs1 = setdiff(locs1,loc + [-1 0 1]);
end
% next, find all sequences composed of purely zeros that remain in vec
% each such sequence MUST be filled with either the sequence [2 3 2 3 ...]
% or the sequence [3 2 3 2 ...]. These are the only possibilities we
% can allow.
remain23 = [reps,reps];
zeroblocks = [strfind([1,vec],[1 0]);strfind([vec,1],[0 1])];
for i = 1:size(zeroblocks,2)
blocklen = diff(zeroblocks);
[blocklen,ind] = max(blocklen);
blocklen = blocklen + 1;
% we need to choose between the subsequences [2 3 2 3...] and [3 2 3 2...]
if mod(blocklen,2) == 0
r = rand > 0.5;
subseq = 2 + mod(r + (1:blocklen),2);
remain23 = remain23 - blocklen/2;
else
r = diff(remain23) < 0;
subseq = 2 + mod(r + (1:blocklen),2);
remain23 = remain23 - floor(blocklen/2);
remain23(2 - r) = remain23(2 - r) - 1;
end
vec(zeroblocks(1,ind):zeroblocks(2,ind)) = subseq;
zeroblocks(:,ind) = [];
end
end
3 Comments
John D'Errico
on 19 Feb 2023
Edited: John D'Errico
on 19 Feb 2023
This is a non-trivial problem to solve, as evidenced by the non-trivial solutions you have been given. By your requirement of non-consecutive elements, That forces the "random" set to be not fully random. Still random in a sense, but strongly non-random in other ways.
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!