how can i have random numbers with criteria?

조회 수: 3 (최근 30일)
itay
itay 2015년 1월 8일
댓글: itay 2015년 1월 9일
i need to have a 50 numbers matrice, while the numbers are between 1 to 5, and i need the neighbouring numbers to be different from each other. for example: 1 - 3 - 4 - 2 - 1 - 2 - 5 - 3 - 4 - 2...
i use:
r = [mod(randperm(50),5)+1];
but it gives me something like this: 1 - 2 - 4 - 4 - 3 - 4 - 5 - 5 - 5 - 3 - 1 - 2 - 2 - ....
how can i make the numbers to be different from the numbers before and after them?
thank you.

답변 (3개)

Guillaume
Guillaume 2015년 1월 8일
Pick any of the 5 numbers for the first elements, for the subsequents, pick any of the four numbers making the set difference between 1:5 and the previous number:
r = [randi(5) randi(4, 1, 49)]; %number 2-50 are indices of which of the four numbers to pick in setdiff.
for idx = 2:numel(r)
rr = setdiff(1:5, r(idx-1)); %exclude previous number
r(idx) = rr(r(idx));
end
  댓글 수: 3
Guillaume
Guillaume 2015년 1월 8일
If all numbers are different from their predecessor doesn't it follow they're also different from their successor?
Image Analyst
Image Analyst 2015년 1월 8일
Uh yeah, dopey me (sound of hand slapping forehead).

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


John D'Errico
John D'Errico 2015년 1월 8일
편집: John D'Errico 2015년 1월 8일
Easy peasy, and vectorized too. No setdiffs needed, no loops, no tests. So it will be quite fast. And essentially one line of code (if we ignore the fact that I defined n and k separately for clarity.)
For the numbers to be different from each other in sequence, this just means that each element must have a difference with its neighbors that is non-zero, modulo 5. I suppose you could also write it as a Markov process, where the state transition matrix is a simple one...
T = (ones(5) - eye(5))/4
T =
0 0.25 0.25 0.25 0.25
0.25 0 0.25 0.25 0.25
0.25 0.25 0 0.25 0.25
0.25 0.25 0.25 0 0.25
0.25 0.25 0.25 0.25 0
Anyway, the simple solution is to pick the first element randomly, then pick a set of random differences, doing arithmetic mod 5. I'll write it for a general set of n numbers, each of which must lie in the set of integers [1:k], but with no consecutive elements that are the same. For your problem, n=50, k=5.
n = 50;
k = 5;
r = 1 + mod(cumsum([randi(k),randi(k-1,[1,n-1])]),k);
r
r =
Columns 1 through 24
4 2 1 3 1 5 2 3 5 4 1 4 2 4 5 2 5 4 1 5 4 3 2 3
Columns 25 through 48
1 2 3 4 5 2 4 3 1 4 1 5 3 4 5 1 2 4 1 2 3 4 5 4
Columns 49 through 50
3 2
As you can see by looking at the difference vector, there are no zero differences.
diff(r)
ans =
Columns 1 through 24
-2 -1 2 -2 4 -3 1 2 -1 -3 3 -2 2 1 -3 3 -1 -3 4 -1 -1 -1 1 -2
Columns 25 through 48
1 1 1 1 -3 2 -1 -2 3 -3 4 -2 1 1 -4 1 2 -3 1 1 1 1 -1 -1
Column 49
-1
And the min and max elements are 1 and 5 respectively.
max(r)
ans =
5
min(r)
ans =
1

Isabella Osetinsky-Tzidaki
Isabella Osetinsky-Tzidaki 2015년 1월 8일
편집: Isabella Osetinsky-Tzidaki 2015년 1월 8일
r=nan(50,1); rng('shuffle') r(1)=randi([1,5]); n=1; while n<50, rng('shuffle') p = randi([1,5]); if p~=r(n), n=n+1; r(n)=p; end end
  댓글 수: 1
John D'Errico
John D'Errico 2015년 1월 8일
Please use the code formatting button, else your answer is unreadable, just a long strung out mess. Just select your text, and click on the little button that says "{} Code".

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

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by