How to randomly shuffle cards?

10 views (last 30 days)
Qilin
Qilin on 27 Mar 2013
So I have 4 decks of cards, and that means I have 208 cards. How to I make a program/function, such that once I start the program/function, it randomly shuffles once, and the cards are not shuffled until there are only 52 cards left. My main problem is to make the order of the randomly shuffled cards fixed, then continue using the order of the cards. When there are only 52 cards left, it should shuffle again, and the cards need to be fixed once again. I really have no idea on how to make this work. Thanks

Answers (2)

Cedric
Cedric on 27 Mar 2013
Edited: Cedric on 27 Mar 2013
Look at the doc for RANDPERM.
You can use it to shuffle a set of 1 to N integers:
>> randperm(12)
ans =
5 8 10 1 9 11 3 7 12 6 2 4
but also e.g. for re-indexing a set of any type of elements..
>> cardsId = [3, 24, 54, 188] ;
>> shuffledCardsId = cardsId(randperm(numel(cardsId)))
shuffledCardsId =
188 3 24 54

Image Analyst
Image Analyst on 27 Mar 2013
Use randperm(). See what you can do with that.
  2 Comments
Qilin
Qilin on 27 Mar 2013
I am actually using the randperm() now. The problem is, the randperm() is in my function, and every time I call the function, it creates a new set of elements. I need to find a way in which the numbers generated from randperm becomes permanent until only 52 cards are left. Then, I shuffle the 208 cards using the randperm again. The newly shuffled cards need to be fixed again until 52 cards are left again.
Image Analyst
Image Analyst on 27 Mar 2013
You're not explaining this well at all. What does "then continue using the order of the cards." mean? I have no idea. What does "becomes permanent until only 52 cards are left." mean? If you want the order permanent, just quit - don't call randperm anymore. Why is the number going down from 208 to 52? What happens to the other cards? Where did they go? You did not say where they went or what happened to them. Are they discarded (removed from the pile)? If so, how? One at a time from the top? Removed from random locations within the deck? If you want specific help you need to explain what is happening in a lot of detail.

Sign in to comment.

Categories

Find more on Communications Toolbox 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!