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
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