Asked by Fabio Masina
on 8 Jul 2019

Hi everybody, I'm struggling with the randomization of the trials presented in my computerized task. I have to present a sequence of 5 numbers (range from 1 to 4) 100 times. I want that each row of the array contains 5 numbers without repeating two times consecutively the same number. Example :

%Wrong pseudorandom

1 2 2 1 4

3 3 4 1 2

1 4 4 4 2

...

%Right pseudorandom

1 2 3 1 4

2 3 4 1 2

1 4 2 4 3

...

Many thanks for any suggestions,

FM

Answer by KALYAN ACHARJYA
on 8 Jul 2019

Edited by KALYAN ACHARJYA
on 8 Jul 2019

Accepted Answer

result1=randperm(4);

result=[result1,result1(randi(3))]

Examples:

result =

4 1 3 2 1

result =

2 3 4 1 3

result =

4 2 1 3 2

result =

3 2 4 1 2

result =

3 2 1 4 2

result =

2 1 3 4 1

result =

2 3 4 1 3

result =

2 4 3 1 4

result =

2 1 3 4 1

result =

1 3 4 2 3

result =

2 3 1 4 3

Is it OK?

Fabio Masina
on 8 Jul 2019

Yes, it is. Just a second question: how can I repeat this sequence 100 times in order to have 100 sequences of numbers?

Again many thanks,

FM

KALYAN ACHARJYA
on 8 Jul 2019

Use loop, while without loop is recomended, if you can manage to do it.

Stephen Cobeldick
on 8 Jul 2019

result1=randperm(4);

result=[result1,result1(2)]

Note that this code always gives exactly the same values in the 2nd and 5th elements.

This repetition does not match the examples given in the question, and would not pass many tests for randomness (because the 5th value can be predicted exactly using the 2nd value).

Sign in to comment.

Answer by Stephen Cobeldick
on 8 Jul 2019

Edited by Stephen Cobeldick
on 8 Jul 2019

>> n = 5;

>> k = 4;

>> t = 100;

>> 1+mod(cumsum([randi(k,t,1),randi(k-1,t,n-1)],2),k)

ans =

3 2 4 2 4

4 2 4 1 3

2 3 4 2 3

2 1 4 1 2

4 1 3 1 4

4 1 4 1 3

1 2 1 2 3

3 4 3 2 1

4 2 3 4 2

2 4 3 1 4

4 2 1 2 1

1 3 4 2 4

2 1 4 1 4

1 3 4 2 3

2 3 1 4 2

... lots of rows here

3 4 2 1 3

3 2 3 1 3

4 1 4 1 4

4 3 2 3 1

4 3 2 4 3

3 4 2 4 1

4 1 4 3 2

Answer by John D'Errico
on 8 Jul 2019

Edited by John D'Errico
on 8 Jul 2019

These things are far easier than you think. In fact, there are (at least) two simple ways to solve it.

Generate multiple sets of 5 numbers. Throw away any that fail the requirements, and then keep the first 100 that make you happy. With only 5 numbers, it won't take that much ovesampling to meet the goals.

R = randi(4,[500,5]);

R(any(diff(R,[],2) == 0,2),:) = [];

size(R)

ans =

164 5

R = R(1:100,:);

As you can see, 500 was about 50% larger of an over-sample than I needed here. If your goals were more stringent, then you would need to do more over-sampling. If the over-sampling was taking too much time, then switch to the second option, here:

Option 2 is to just use a loop. Generate the first number as anything from 1-4. Then choose the second number as anything that is not a repeat of the number that precedes it in the list. There is no oversampling required here, just a simple loop. The nice thing is, this will work for ANY set of numbers, of any length. So if you needed sets of 10000 numbers that do not repeat, it is trivial to write.

The point is, either of those ways is trivial to write. Perhaps you think there is some absolutely obvious vectorized way that you are failing to see. (In fact, it looks like there is, as pointed out by Stephen, reflecting a line of code I wrote some time ago.) But the point is, you are over-thinking the problem, looking for some magical solution that does what you want in one brilliant line of code, while tearing your hair out in not seeing it. Your code does not need to be brilliant. It needs to be functional, while not wasting your time in trying to write it.

Stephen Cobeldick
on 8 Jul 2019

"...looking for some magical solution that does what you want in one brilliant line of code"

Option three: use the "one brilliant line" from John D'Errico, shown in my answer!

Sign in to comment.

Answer by Fabio Masina
on 8 Jul 2019

Thanks for your precious suggestions.

Sorry for the triviality of my question. I'm an inexpert Matlab user.

Anyway, thank you :)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.