Card dealing Macro not random enough

Rookie1

New Member
Joined
Mar 22, 2006
Messages
16
Excel 2007, WindowsXP SP3

Hi board. The included macro(s) deal out a deck of cards onto my excel sheet. Run the macro again and it deals out another set on another sheet. The problem is that when you close the file without saving, (I have no need to keep the info) and reopen it, the same cards are dealt each time. 1st run of the macro returns the same cards, in the same order as the last time I had the file open. 2nd run, same story. What I am trying to attain is a "freshly shuffled" condition that returns totally randomized returns every time. Is this possible? Maybe one of you has a better way? Any help is much appreciated.
Here's the code:

Option Explicit

Sub Poker_Dict()
' Requires a reference to the Microsoft Scripting Runtime
Dim NumCards As Integer, Players As Integer
Dim Suits(), Cards()
Dim J As Variant, K As Variant
Dim CardNum As Integer, i As Integer, v As Integer, CardPick As Integer
Dim Casino As Dictionary, CardName As String
Dim NewSheet As Worksheet

Set Casino = New Dictionary
' number of cards
NumCards = 5
' number of players
Players = 10

If NumCards * Players > 52 Then
MsgBox "You have exceeded one deck!", vbCritical
Exit Sub
End If

Application.ScreenUpdating = False

'Add a new sheet for the game
Set NewSheet = ActiveWorkbook.Sheets.Add

'Requires Excel 2000+ to use Array
Suits = Array("Spades", "Clubs", "Diamonds", "Hearts")
Cards = Array("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", _
"Ten", "Jack", "Queen", "King")

' Add the cards to the Dictionary Object.
i = 1
For Each J In Suits
For Each K In Cards
Casino.Add K & " of " & J, i
i = i + 1
Next K
Next J

'Pick a random card, deal it and remove it from the pack
For i = 1 To Players
NewSheet.Cells(1, i) = "Player " & i
For v = 1 To NumCards
CardPick = Int(Rnd() * Casino.Count)
CardName = Casino.keys(CardPick)
NewSheet.Cells(v + 1, i) = CardName
Casino.Remove (CardName)
Next v
Next i

'dump undealt cards
v = 1
NewSheet.Cells(v, i + 1) = "Undealt Cards"
For Each J In Casino
v = v + 1
NewSheet.Cells(v, i + 1) = J
Next J

'Autofit columns
NewSheet.UsedRange.EntireColumn.AutoFit

'show the result
Application.ScreenUpdating = True

Set Casino = Nothing

End Sub


Sub Poker_Coll()
'Uses a collection not a dictionary
Dim NumCards As Integer, Players As Integer
Dim Suits(), Cards()
Dim J As Variant, K As Variant
Dim CardNum As Integer, i As Integer, v As Integer, CardPick As Integer
Dim Casino As Collection, CardName As String
Dim NewSheet As Worksheet


Set Casino = New Collection
' number of cards
NumCards = 5
' number of players
Players = 10

If NumCards * Players > 52 Then
MsgBox "You have exceeded one deck!", vbCritical
Exit Sub
End If

Application.ScreenUpdating = False

'Add a new sheet for the game
Set NewSheet = ActiveWorkbook.Sheets.Add

'Requires Excel 2000+ to use Array
Suits = Array("Spades", "Clubs", "Diamonds", "Hearts")
Cards = Array("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", _
"Ten", "Jack", "Queen", "King")

' Add the cards to the Collection
i = 1
For Each J In Suits
For Each K In Cards
Casino.Add K & " of " & J
i = i + 1
Next K
Next J

'Pick a random card, deal it and remove it from the pack
For i = 1 To Players
NewSheet.Cells(1, i) = "Player " & i
For v = 1 To NumCards
CardPick = Int(Rnd() * Casino.Count + 1)
CardName = Casino(CardPick)
NewSheet.Cells(v + 1, i) = CardName
Casino.Remove (CardPick)
Next v
Next i

'dump undealt cards
v = 1
NewSheet.Cells(v, i + 1) = "Undealt Cards"
For Each J In Casino
v = v + 1
NewSheet.Cells(v, i + 1) = J
Next J

NewSheet.UsedRange.EntireColumn.AutoFit

Application.ScreenUpdating = True

Set Casino = Nothing

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Put Randomize before each occurance of Rnd()
Code:
'Pick a random card, deal it and remove it from the pack
For i = 1 To Players 
    NewSheet.Cells(1, i) = "Player " & i 
    For v = 1 To NumCards
        [COLOR="Red"]Randomize[/COLOR]
        CardPick = Int(Rnd() * Casino.Count) 
        CardName = Casino.keys(CardPick) 
        NewSheet.Cells(v + 1, i) = CardName 
        Casino.Remove (CardName) 
    Next v 
Next i
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
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