Simulator for one event in blackjack game

baha17

Board Regular
Joined
May 12, 2010
Messages
183
Greetings All,

Below is the code to run a simulation for blackjack game with 6 decks. My objective is to find the possible outcome of when the player gets Queen and 6. The simulator should run one card to player, one card to dealer then one card to player again. So the first and the third cards should go to the player while second card goes to dealer. I use the simulator to confirm my math calculation but it seems like my code run error. Can someone help me out?
Thanks in advance

VBA Code:
Sub SimulateQueensAndDiamondWithSuits()

  ' Define constants
  Const NUM_DECKS As Integer = 6
  Const NUM_CARDS_PER_SUIT As Integer = 13 ' Ace, 2-10, Jack, Queen, King
  Const NUM_SUITS As Integer = 4 ' Diamonds, Spades, Clubs, Hearts
  Const NUM_SIMULATIONS As Variant = 1000000 ' 1 million hands

  ' Initialize variables
  Dim successes As Integer
  successes = 0
  Dim deck() As Variant ' Array to hold all cards in the shoe
  Dim hand(1 To 3) As Variant ' Array to hold a hand (3 cards)
  Dim i As Long, j As Long, sim As Long, suit As Integer, cardValue As Integer

  ' Initialize random number generator
  Randomize

  ' Create a complete shoe (combine all decks)
  ReDim deck(1 To NUM_DECKS * NUM_SUITS * NUM_CARDS_PER_SUIT)
  cardValue = 0
  For suit = 1 To NUM_SUITS
    For i = 1 To NUM_CARDS_PER_SUIT
      cardValue = cardValue + 1
      deck((suit - 1) * NUM_CARDS_PER_SUIT + i) = Array(cardValue, suit) ' Store card value and suit
    Next i
  Next suit

  ' Simulation loop
  For sim = 1 To NUM_SIMULATIONS

    ' Shuffle the shoe (using Fisher-Yates shuffle)
    For i = UBound(deck) - 1 To 2 Step -1
      j = Int(Rnd() * (i + 1)) ' Random index within remaining cards
      temp = deck(i)
       deck(i) = deck(j)
      deck(j) = temp
    Next i

    ' Draw three cards (without resizing yet)
    For i = 1 To 3
      hand(i) = deck(UBound(deck)) ' Pick top card from shoe
    Next i

    ' Resize deck array to remove drawn cards (once)
    ReDim Preserve deck(1 To UBound(deck) - 3)

    ' Check if first card is Queen and third card is Six of Diamonds (or vice versa)
    If (hand(1)(0) = 12 And hand(3)(0) = 6 And hand(3)(1) = Diamond) Or _
       (hand(1)(0) = 6 And hand(3)(0) = 12 And hand(3)(1) = Diamond) Then
      successes = successes + 1
    End If

  Next sim

  ' Calculate probability
  probability = successes / NUM_SIMULATIONS

  ' Display results in a message box
  MsgBox "Number of hands simulated: " & NUM_SIMULATIONS & vbCrLf & _
         "Number of successful hands: " & successes & vbCrLf & _
         "Probability: " & FormatPercent(probability, 4)

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I'm not sure what the point of the exercise is. You're testing only the first three cards drawn from a new set of decks. With successive draws, the probabilities of any particular outcome will vary depending on previous draws. Hence the point of counting cards (if you can get away with it).

And wouldn't a more meaningful probability be a ten-value plus a 6, rather than a Queen (specifically) and a 6?

Anyway ... based on a very quick review only, there are a few obvious problems with your code:

VBA Code:
'These four lines:
cardValue = 0
For suit = 1 To NUM_SUITS
    For i = 1 To NUM_CARDS_PER_SUIT
        cardValue = cardValue + 1
'Should be
For suit = 1 To NUM_SUITS
    cardValue = 0
    For i = 1 To NUM_CARDS_PER_SUIT
        cardValue = cardValue + 1
'otherwise cardValue will keep increasing beyond 13!

Code:
'This loop will draw the same card for hand 1, 2, and 3.
    For i = 1 To 3
      hand(i) = deck(UBound(deck)) ' Pick top card from shoe
    Next i

    ' Resize deck array to remove drawn cards (once)
    ReDim Preserve deck(1 To UBound(deck) - 3)

Instead, you can simply set hand(i)=deck(i) (given that you have shuffled). And there is no need to ReDim deck.

It's not clear why you're checking for diamonds in the code snippet below? You said you wanted to test for Queen and 6, no mention of suit?
Have you defined diamond somewhere as a number?
And your code is actually checking (Q & 6D) OR (6 and QD)

Code:
If (hand(1)(0) = 12 And hand(3)(0) = 6 And hand(3)(1) = diamond) Or _
   (hand(1)(0) = 6 And hand(3)(0) = 12 And hand(3)(1) = diamond) Then

If you are actually checking for Q and 6 of any suit, your probability should be around 1.19%.

ABCD
1Cards/deck52
2Card values 13
3No. decks6
4No cards per card value24
5
6Total cards312
7CombinsCheck Total
8Prob. Any 2 different card values1.19%7892.60%
9Prob. 2 matching card values0.57%137.40%
10100.00%
Sheet1
Cell Formulas
RangeFormula
B4B4=B1/B2*B3
B6B6=B3*B1
B8B8=2*B4*(B4*(B4-1)+(B6-1-B4)*B4)/(B6*(B6-1)*(B6-2))
C8C8=COMBIN(13,2)
B9B9=B4*((B4-1)*(B4-2)+(B6-B4)*(B4-1))/(B6*(B6-1)*(B6-2))
C9C9=COMBIN(13,1)
D8:D9D8=C8*B8
D10D10=SUM(D8:D9)
 
Upvote 0
Hello Stephen,
I couldn't try the code yet but thank you very much for your reply. Yes it has to be Queen and 6
Get back to you after I get my laptop.
Have a great day
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,458
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top