shuffling cards using only simple cuts

조회 수: 1 (최근 30일)
Tim Bentley
Tim Bentley 2020년 4월 15일
답변: the cyclist 2020년 4월 15일
Hello,
I am trying to write a code to split a deck of cards at a random point, then join the two halves back together with half 2 in front of half 1 instead of behind. This is where my code is at right now:
clear
clc
Deck = ["D1" "D2" "D3" "D4" "D5" "D6" "D7" "D8" "D9" "D10" "DJ" "DQ" "DK" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "HJ" "HQ" "HK" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "CJ" "CQ" "CK" "S1" "S2" "S3" "S4" "S5" "S6" "S7" "S8" "S9" "S10" "SJ" "SQ" "SK"];
Deckinit = Deck;
for n = 1:1
split = randi([2 length(Deck)-1],1,1);
Deck1 = zeros(1,split);
Deck2 = zeros(1,length(Deck)-split);
for k = 1:split
Deck1(k) = Deck(k);
end
for k = split+1:length(Deck)
Deck2(k - split) = Deck(k);
end
Deck = [Deck2 Deck1];
end
The only issue is, sometimes it runs and Deck is 1x52, which is what I expect, and sometimes it includes NaN elements and is of wildly different lengths. I think it has something to do with some values for split working and some not. Any and all help is appreciated!
  댓글 수: 1
Tim Bentley
Tim Bentley 2020년 4월 15일
clear
clc
Deck = ["D1" "D2" "D3" "D4" "D5" "D6" "D7" "D8" "D9" "D10" "DJ" "DQ" "DK" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "HJ" "HQ" "HK" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "CJ" "CQ" "CK" "S1" "S2" "S3" "S4" "S5" "S6" "S7" "S8" "S9" "S10" "SJ" "SQ" "SK"];
Deckinit = Deck;
for n = 1:1
split = randi([2 length(Deck)-1],1,1);
for k = 1:split
Deck1(k) = Deck(k);
end
for k = split+1:length(Deck)
Deck2(k - split) = Deck(k);
end
Deck = [Deck2 Deck1];
end
removing the two zeros() lines is more computationally expensive but stops the NaN issue

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

채택된 답변

the cyclist
the cyclist 2020년 4월 15일
split = randi(numel(Deck)-1);
Deck = [Deck(split+1:end),Deck(1:split)];

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by