random numbers without repetition of two consecutive numbers

조회 수: 25 (최근 30일)
Fabio Masina
Fabio Masina 2019년 7월 8일
편집: KALYAN ACHARJYA 2019년 7월 8일
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

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 8일
편집: KALYAN ACHARJYA 2019년 7월 8일
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?
  댓글 수: 3
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 8일
Use loop, while without loop is recomended, if you can manage to do it.
Stephen23
Stephen23 2019년 7월 8일
편집: Stephen23 2019년 7월 8일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Stephen23
Stephen23 2019년 7월 8일
편집: Stephen23 2019년 7월 8일
>> 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

John D'Errico
John D'Errico 2019년 7월 8일
편집: John D'Errico 2019년 7월 8일
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.
  댓글 수: 1
Stephen23
Stephen23 2019년 7월 8일
편집: Stephen23 2019년 7월 8일
"...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!

댓글을 달려면 로그인하십시오.


Fabio Masina
Fabio Masina 2019년 7월 8일
Thanks for your precious suggestions.
Sorry for the triviality of my question. I'm an inexpert Matlab user.
Anyway, thank you :)

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by