How Can I Select a Random Subset of Cards in MATLAB for Blackjack?

  • Thread starter physizl
  • Start date
  • Tags
    Matlab
In summary, The conversation discusses writing a code for a user to play a game of blackjack. The code involves creating a deck of cards and obtaining a random subset of two cards from the deck. The conversation also addresses an issue with the for loop in the code.
  • #1
physizl
9
0

Homework Statement



write a code for a user to play a game of blackjack

The Attempt at a Solution



I have

deck = ['AH';'2H';'3H';'4H';'5H';'6H';'7H';'8H';'9H';'TH';'JH';'QH';'KH';
'AS';'2S';'3S';'4S';'5S';'6S';'7S';'8S';'9S';'TS';'JS';'QS';'KS';
'AD';'2D';'3D';'4D';'5D';'6D';'7D';'8D';'9D';'TD';'JD';'QD';'KD';
'AC';'2C';'3C';'4C';'5C';'6C';'7C';'8C';'9C';'TC';'JC';'QC';'KC'];

but how do i get a random subset of 2 cards from this matrix??
tried randperm and randi... no luck :(

help me please!
 
Physics news on Phys.org
  • #2
try

Code:
rnum = round(n*rand(1,2));
card1 = deck(rnum(1));
card2 = deck(rnum(2));

Where n is the number of cards you want to sample.
 
  • #3
thanks that helps

so now i have:

Code:
h1 = round(12*rand(1,2))+1;
disp(['Dealer deals you: ' , num2str(h1) ] )
d1 = input('hit or pass? ');
for hit
    h2 = round(12*rand(1))+1; % user's new hand
    h12 = [h1 h2];
    disp(['Dealer deals... you get: ' , num2str(h12) ]) 
end

i have round(12*rand(1,2))+1 because round(13*rand(1,2)) gives me a zero sometimes.
what's wrong with my for loop? when i type hit for input, i get an error
 
  • #4
Your for loop was developed incorrectly it should read:

Code:
for j=1:hit
    h2 = round(12*rand(1))+1; % user's new hand
    h12 = [h1 h2];
    disp(['Dealer deals... you get: ' , num2str(h12) ]) 
end
 
  • #5


I would suggest using the built-in function randperm to generate a random permutation of the indices of the deck matrix. This will give you a random subset of cards from the deck. You can then use these indices to access the corresponding cards in the deck matrix and assign them to the player's hand. Additionally, you may want to consider incorporating a loop to ensure that the same card is not selected twice. I recommend consulting the MATLAB documentation for more information on how to effectively use the randperm function. Good luck with your code!
 

Related to How Can I Select a Random Subset of Cards in MATLAB for Blackjack?

1. What is the purpose of playing Blackjack with MATLAB?

The purpose of playing Blackjack with MATLAB is to simulate a real-life game of Blackjack and analyze different strategies and outcomes. It also allows for data analysis and testing of various betting systems.

2. How does MATLAB help in playing Blackjack?

MATLAB provides a platform for programming and simulating a game of Blackjack. It allows for the creation of custom algorithms, analysis of data and statistics, and visualization of results.

3. Can MATLAB be used to develop a winning Blackjack strategy?

Yes, MATLAB can be used to develop a winning Blackjack strategy. By analyzing data and running simulations, it can help identify the most optimal strategy for maximizing winnings and minimizing losses.

4. Is it necessary to have prior knowledge of Blackjack to use MATLAB for playing?

No, it is not necessary to have prior knowledge of Blackjack to use MATLAB for playing. However, some understanding of the rules and basic strategy may be helpful in developing and testing different algorithms.

5. Can MATLAB be used to play Blackjack in real-time?

No, MATLAB is not designed for real-time gameplay. It is primarily used for data analysis and simulations. However, it can be used to analyze real-time data from a game of Blackjack and provide insights and strategies for future games.

Back
Top