approximating a shuffle with excell

Canceler

Well-Known Member
If you're doing what I think you might be doing, I thought that method of producing a random shuffle was fairly ingenious.

Trying to approximate a real shuffle would be a whole 'nother story.
 

sagefr0g

Well-Known Member
Canceler said:
If you're doing what I think you might be doing, I thought that method of producing a random shuffle was fairly ingenious.

Trying to approximate a real shuffle would be a whole 'nother story.
just was associating each individual color coded physical representation of a card (color coded by + or - tag value & no color for neutral cards) with either a rand() or a randbetween() function. then i'd select all those cells after the random functions calculate and sort them from highest number to lowest number. and also let the tagged card values be summed up and divided by the number of decks stipulated to be dealt (in one's mind so to speak).... to calculate the true count.
but yeah it would be just a random sort of thing i guess not the same thing as a real physical casino shuffle.
i guess there is two things i don't get. the first is what would the differance in randomness be between using randbetween() and just rand() functions. and the other would be what's the differance between a real physical casino shuffle and a random shuffle? :confused:
 

Canceler

Well-Known Member
sagefr0g said:
sort them from highest number to lowest number
Using a left to right sort?

sagefr0g said:
what would the differance in randomness be between using randbetween() and just rand() functions.
Either one would probably work for your purpose. Randbetween() might give you some duplicate numbers, but I'm not sure that even matters much. Personally, I would probably use rand().

sagefr0g said:
what's the differance between a real physical casino shuffle and a random shuffle?
A shoe full of essentially random cards is usually what we're faced with. I was just thinking shuffle-trackers or sequencers might want to simulate a casino shuffle.
 

sagefr0g

Well-Known Member
Canceler said:
Using a left to right sort?
yes sorting by rows.
Canceler said:
Either one would probably work for your purpose. Randbetween() might give you some duplicate numbers, but I'm not sure that even matters much. Personally, I would probably use rand().
thank you Canceler i appreciate the info. i suspected as much but just didn't know.
Canceler said:
A shoe full of essentially random cards is usually what we're faced with. I was just thinking shuffle-trackers or sequencers might want to simulate a casino shuffle.
yeah i wasn't shooting for a ST type of thing. so i don't know to me anything is really random lol but some phenomenon have these predictable repeated orders and the like. as i understand it most rng's really do but it's about the best a computer can do. but anyway to me just all a shuffle is comes down to mixing up the cards and of course there is some way the casino stipulates the dealer does that. but i think the idea behind any shuffle is to mix the cards up more or less randomly. so well thanks for the help.
 

k_c

Well-Known Member
sagefr0g said:
just curious if anyone knows a proper way to approximate a shuffle with excell. i tryed to do it using the functions randbetween() and rand() .
http://www.blackjackinfo.com/bb/showpost.php?p=71183&postcount=33
but i don't even know if thats a legitimate way of going about it or not?
One way to do it:

0. Open Excel
1. Press alt + F11 to open Visual Basic Editor
2. Click Insert Menu and choose Module
3. Paste this code into the module:
Code:
Option Explicit

Global TheDeck() As Integer       'we'll allow for up to 8 decks later
Global DeckTot As Integer         'keep track of number of cards in deck
Global TimesToShuffle As Integer  'number of times to shuffle deck

Public Function InitDeck() As Integer
Dim i As Integer
Dim j As Integer

If DeckTot = 0 Then Exit Function

    'place the cards in order in the deck
    For i = 0 To DeckTot - 1
        TheDeck(i) = i
    Next i
    
    'if value of card is higher than 51 then
    'subtract 52 to start a new deck
    For i = 0 To DeckTot
        For j = 1 To DeckTot / 52
            If (TheDeck(i) > 51) Then
                TheDeck(i) = TheDeck(i) - 52
            End If
        Next j
    Next i
    
    'return values
    InitDeck = TheDeck(DeckTot)
    
End Function

Public Sub doInitialize(nDecks As Long)
Dim intNumDecks As Integer
    'initialize number of decks (change this value as needed)
    intNumDecks = CInt(nDecks)
    
        'if programmer is trying to use too many
        'decks let him know now and require an input of 1 to 8
        If intNumDecks > 8 Then
            MsgBox "You are trying to use too many decks", vbOKOnly, "Error"
            Shuffle
        End If
    
    'calculate total number of cards
    DeckTot = intNumDecks * 52
End Sub

Public Function CardShuff(intNumCardsDeck As Integer, intTimesToShuffle As Integer) As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim intHolder As Integer
    
    'randomize
    Randomize Timer
    
    'shuffle cards (intTimesToShuffle) times
    'by picking a two random cards and having
    'them trade places in the deck.
    'loop it through each card in the deck.
     For k = 1 To intTimesToShuffle
        For i = 0 To intNumCardsDeck - 1
            j = Int(Rnd * intNumCardsDeck)
            
            intHolder = TheDeck(i)              'these lines
            TheDeck(i) = TheDeck(j)         'swap the cards
            TheDeck(j) = intHolder          'places
        Next i
    Next k
    
    'return values
    CardShuff = TheDeck(DeckTot)
End Function

Public Sub Shuffle()
    Dim nDecks As Long
    
    nDecks = InputBox("Enter number of decks (1 to 8)", "Number of Decks")
    doInitialize nDecks
    ReDim TheDeck(52 * nDecks)
    DeckTot = CInt(52 * nDecks)
    InitDeck
    TimesToShuffle = InputBox("Input number of times to shuffle", "Number of Shuffles")
    
    TheDeck(DeckTot) = CardShuff(DeckTot, TimesToShuffle)
    Stop
    'Let n=(cards-1), where cards=52*decks
    'If cards=1, reference top card
    'If cards=2, reference second card
    'If cards=n, reference nth card
    'At this point TheDeck(n) holds these cards
    'If TheDeck(n)=(0 to 3) the card is an ace
    'If TheDeck(n)=(4 to 7) the card is a two
    'If TheDeck(n)=(8 to 11) the card is a three
    'If TheDeck(n)=(12 to 15) the card is a four
    'If TheDeck(n)=(16 to 19) the card is a five
    'If TheDeck(n)=(20 to 23) the card is a six
    'If TheDeck(n)=(24 to 27) the card is a seven
    'If TheDeck(n)=(28 to 31) the card is an eight
    'If TheDeck(n)=(32 to 35) the card is a nine
    'If TheDeck(n)=(36 to 51) the card is a ten
End Sub
4. Run from Tools/Macro/Shuffle menu item
5. You will need to input a valid number of decks and times to shuffle.

Program will stop at a point where you can view the results. What you wind up with is an array of randomly shuffled cards of 52*decks elements. Go to View Menu/Immediate Window. In the Immediate Window type "Print TheDeck(n)" (without the quotations) where n is any number between 0 to 52*decks-1 to display the card in position (n+1) of the shuffled deck. It's up to you how to use the array in a program.

Hope I didn't make any errors so it works.

k_c
 
Last edited:

sagefr0g

Well-Known Member
k_c said:
One way to do it:

.......Hope I didn't make any errors so it works.

k_c
lol .... ok thanks k_c !
i'll try it out after i quit laughing. don't mean to be offensive in any way, it just kind of tickles me. :)
thanks again i'll let you know how it turns out.
 

sagefr0g

Well-Known Member
hey k_c

ok k_c i tryed to follow your directions.
got flaged with a syntax error. not sure how come.
err, i never did enter a valid number of decks & times to shuffle.
was i supposed to do that before running the program? :confused:

public sub shuffle was high lited......???

lost here really lost lol.
 

Kasi

Well-Known Member
sagefr0g said:
what's the differance between a real physical casino shuffle and a random shuffle?
I thought Wong's single-deck shuffle study was interesting in that it seemed in a "purely random", say computer shuffle, the gap between 2 cards that were adjacent to each other in the prior shoe would tend to have an even uniform number of cards between them in the next shoe. But what happened was that, after the dealer shuffle" the expected gap was much smaller and revealed through chi-square tests that it just wasn't possible to be so skewed and still random. So that in this particular, narrow respect, the cards were definitely not shuffled enough to produce "random" results.

But, on the other hand, I think he also concluded the cards were "random enough" in the sense it made no difference in results, ignoring possible tracking advantages etc lol.

Somewhere in the past Sonny and I and others had a discussion about it.
 

sagefr0g

Well-Known Member
Kasi said:
I thought Wong's single-deck shuffle study was interesting in that it seemed in a "purely random", say computer shuffle, the gap between 2 cards that were adjacent to each other in the prior shoe would tend to have an even uniform number of cards between them in the next shoe. But what happened was that, after the dealer shuffle" the expected gap was much smaller and revealed through chi-square tests that it just wasn't possible to be so skewed and still random. So that in this particular, narrow respect, the cards were definitely not shuffled enough to produce "random" results.

But, on the other hand, I think he also concluded the cards were "random enough" in the sense it made no difference in results, ignoring possible tracking advantages etc lol.

Somewhere in the past Sonny and I and others had a discussion about it.
thanks Kasi. and i just skim read that section of Wong's. and i guess when you think about it a computer random shuffle probably could and would do things that turn out impossible compared to say if you shuffle cards by hand that start out with some certain order and all.
 

k_c

Well-Known Member
sagefr0g said:
ok k_c i tryed to follow your directions.
got flaged with a syntax error. not sure how come.
err, i never did enter a valid number of decks & times to shuffle.
was i supposed to do that before running the program? :confused:

public sub shuffle was high lited......???

lost here really lost lol.
Well, yes you need to enter valid numbers for both. There is no error checking so a non-numeric number causes an error. If you get by those 2 inputs, the program will stop so you can inspect the output. I could have just about as easily just created a display of the 52, 104, 156, 208, 260, 312, 364, or 416 cards, depending on number of decks input, as they were shuffled, but I thought if you saw the actual array it would be better so you could use it. All that would need to be done to use the Shuffle Macro in Excel is to communicate what is in the array to Excel using VBA.

k_c
 

sagefr0g

Well-Known Member
k_c said:
Well, yes you need to enter valid numbers for both. There is no error checking so a non-numeric number causes an error. If you get by those 2 inputs, the program will stop so you can inspect the output. I could have just about as easily just created a display of the 52, 104, 156, 208, 260, 312, 364, or 416 cards, depending on number of decks input, as they were shuffled, but I thought if you saw the actual array it would be better so you could use it. All that would need to be done to use the Shuffle Macro in Excel is to communicate what is in the array to Excel using VBA.

k_c
ok but i'm still confused. i mean when i try and run the program no input box presents so that i can enter #decks or # shuffles.....
or do i need to insert those numbers in your code before running the program.
sorry, hope i'm not to much of a bother. take your time or what ever lol.
 

k_c

Well-Known Member
sagefr0g said:
ok but i'm still confused. i mean when i try and run the program no input box presents so that i can enter #decks or # shuffles.....
or do i need to insert those numbers in your code before running the program.
sorry, hope i'm not to much of a bother. take your time or what ever lol.
OK, maybe you don't have all of VBA enabled or something so the InputBox isn't available. I hard coded number of decks and times to shuffle and added an output to Excel to display up to 416 cards in cell A1 to A416, depending on decks. You can manually change decks and times to shuffle in the code.

Also try Tools/References in the Visual Basic Editor and make sure Visual Basic For Applications is checked. Maybe that will allow an InputBox, but I'm not sure.
Code:
Option Explicit

Global TheDeck() As Integer       'allow for up to 8 decks
Global DeckTot As Integer         'keep track of number of cards in deck
Global TimesToShuffle As Integer  'number of times to shuffle deck

Public Function InitDeck() As Integer
Dim i As Integer
Dim j As Integer

If DeckTot = 0 Then Exit Function

    'place the cards in order in the deck
    For i = 0 To DeckTot - 1
        TheDeck(i) = i
    Next i
    
    'if value of card is higher than 51 then
    'subtract 52 to start a new deck
    For i = 0 To DeckTot
        For j = 1 To DeckTot / 52
            If (TheDeck(i) > 51) Then
                TheDeck(i) = TheDeck(i) - 52
            End If
        Next j
    Next i
    
    'return values
    InitDeck = TheDeck(DeckTot)
    
End Function

Public Sub doInitialize(nDecks As Long)
Dim intNumDecks As Integer
    'initialize number of decks (change this value as needed)
    intNumDecks = CInt(nDecks)
    
        'if programmer is trying to use too many
        'decks let him know now and require an input of 1 to 8
        If intNumDecks > 8 Then
            MsgBox "You are trying to use too many decks", vbOKOnly, "Error"
            End
        End If
    
    'calculate total number of cards
    DeckTot = intNumDecks * 52
End Sub

Public Function CardShuff(intNumCardsDeck As Integer, intTimesToShuffle As Integer) As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim intHolder As Integer
    
    'randomize
    Randomize Timer
    
    'shuffle cards (intTimesToShuffle) times
    'by picking a two random cards and having
    'them trade places in the deck.
    'loop it through each card in the deck.
     For k = 1 To intTimesToShuffle
        For i = 0 To intNumCardsDeck - 1
            j = Int(Rnd * intNumCardsDeck)
            
            intHolder = TheDeck(i)              'these lines
            TheDeck(i) = TheDeck(j)         'swap the cards
            TheDeck(j) = intHolder          'places
        Next i
    Next k
    
    'return values
    CardShuff = TheDeck(DeckTot)
End Function

Public Sub Shuffle()
    Dim nDecks As Long
    
    'nDecks = InputBox("Enter number of decks (1 to 8)", "Number of Decks")
    'nDecks = CLng(UserForm1.TextBox1.Text)
    nDecks = 2
    doInitialize nDecks
    ReDim TheDeck(52 * nDecks)
    DeckTot = CInt(52 * nDecks)
    InitDeck
    'TimesToShuffle = InputBox("Input number of times to shuffle", "Number of Shuffles")
    'TimesToShuffle = CInt(UserForm1.TextBox2.Text)
    TimesToShuffle = 3
    
    TheDeck(DeckTot) = CardShuff(DeckTot, TimesToShuffle)
    
    Dim n As Integer
    Dim rank As Integer
    
    Worksheets("Sheet1").Activate
    Worksheets("Sheet1").Range("A1:A416").ClearContents
    For n = 0 To DeckTot - 1
        If TheDeck(n) < 36 Then
            Worksheets("Sheet1").Cells(n + 1, 1).Value = TheDeck(n) \ 4 + 1
        Else
            Worksheets("Sheet1").Cells(n + 1, 1).Value = 10
        End If
    Next n
    Stop
    'Let n=(cards-1), where cards=52*decks
    'If cards=1, reference top card
    'If cards=2, reference second card
    'If cards=n, reference nth card
    'At this point TheDeck(n) holds these cards
    'If TheDeck(n)=(0 to 3) the card is an ace
    'If TheDeck(n)=(4 to 7) the card is an two
    'If TheDeck(n)=(8 to 11) the card is an three
    'If TheDeck(n)=(12 to 15) the card is an four
    'If TheDeck(n)=(16 to 19) the card is an five
    'If TheDeck(n)=(20 to 23) the card is an six
    'If TheDeck(n)=(24 to 27) the card is an seven
    'If TheDeck(n)=(28 to 31) the card is an eight
    'If TheDeck(n)=(32 to 35) the card is an nine
    'If TheDeck(n)=(36 to 51) the card is an ten
End Sub
k_c
 
Last edited:

Kasi

Well-Known Member
sagefr0g said:
what types of things does excel's rng do that undermines it's usefulness?
Not sure but I know, years ago, when I used the Random function in Lotus to pick my lottery ticket numbers I eventually discovered it would give the same set of random numbers the first time I made it do another set of numbers assuming I began with the same numbers in the first place.

In other words it would reset itself if you began from square 1.

No wonder I never won lol.

In other words if your 52 cards were originally 1 thru 52 sequentially, and you used the function the first time what got generated the first time was identical if you put the cards back in 1 thru 52 order and did it again.

Not very clear here I realize but I think some programmer made use of the same thing when some casino would reset their Keno game every morning and then it would always draw the same set of numbers for the first game, 2nd game etc. as the day before. Idiot got a little greedy so they caught him.

Something about the initial seed or something lol.
 

k_c

Well-Known Member
Kasi said:
Not sure but I know, years ago, when I used the Random function in Lotus to pick my lottery ticket numbers I eventually discovered it would give the same set of random numbers the first time I made it do another set of numbers assuming I began with the same numbers in the first place.

In other words it would reset itself if you began from square 1.

No wonder I never won lol.

In other words if your 52 cards were originally 1 thru 52 sequentially, and you used the function the first time what got generated the first time was identical if you put the cards back in 1 thru 52 order and did it again.

Not very clear here I realize but I think some programmer made use of the same thing when some casino would reset their Keno game every morning and then it would always draw the same set of numbers for the first game, 2nd game etc. as the day before. Idiot got a little greedy so they caught him.

Something about the initial seed or something lol.
Right. As long as the seed is the same the same set of data is generated. That can be helpful in debugging a program or for other reasons. What is usually done to generate a different set of data is to make the seed dependent upon the system time so every time data is generated it will be with a different seed.

k_c
 

sagefr0g

Well-Known Member
appreciate it very much k_c .
still getting a syntax error. uhmm and Visual Basic For Applications is checked.
not to worry i'm sure we'll get it. sure do appreciate it again. thanks much.

k_c said:
OK, maybe you don't have all of VBA enabled or something so the InputBox isn't available. I hard coded number of decks and times to shuffle and added an output to Excel to display up to 416 cards in cell A1 to A416, depending on decks. You can manually change decks and times to shuffle in the code.

Also try Tools/References in the Visual Basic Editor and make sure Visual Basic For Applications is checked. Maybe that will allow an InputBox, but I'm not sure.
Code:
Option Explicit

Global TheDeck() As Integer       'allow for up to 8 decks
Global DeckTot As Integer         'keep track of number of cards in deck
Global TimesToShuffle As Integer  'number of times to shuffle deck

Public Function InitDeck() As Integer
Dim i As Integer
Dim j As Integer

If DeckTot = 0 Then Exit Function

    'place the cards in order in the deck
    For i = 0 To DeckTot - 1
        TheDeck(i) = i
    Next i
    
    'if value of card is higher than 51 then
    'subtract 52 to start a new deck
    For i = 0 To DeckTot
        For j = 1 To DeckTot / 52
            If (TheDeck(i) > 51) Then
                TheDeck(i) = TheDeck(i) - 52
            End If
        Next j
    Next i
    
    'return values
    InitDeck = TheDeck(DeckTot)
    
End Function

Public Sub doInitialize(nDecks As Long)
Dim intNumDecks As Integer
    'initialize number of decks (change this value as needed)
    intNumDecks = CInt(nDecks)
    
        'if programmer is trying to use too many
        'decks let him know now and require an input of 1 to 8
        If intNumDecks > 8 Then
            MsgBox "You are trying to use too many decks", vbOKOnly, "Error"
            End
        End If
    
    'calculate total number of cards
    DeckTot = intNumDecks * 52
End Sub

Public Function CardShuff(intNumCardsDeck As Integer, intTimesToShuffle As Integer) As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim intHolder As Integer
    
    'randomize
    Randomize Timer
    
    'shuffle cards (intTimesToShuffle) times
    'by picking a two random cards and having
    'them trade places in the deck.
    'loop it through each card in the deck.
     For k = 1 To intTimesToShuffle
        For i = 0 To intNumCardsDeck - 1
            j = Int(Rnd * intNumCardsDeck)
            
            intHolder = TheDeck(i)              'these lines
            TheDeck(i) = TheDeck(j)         'swap the cards
            TheDeck(j) = intHolder          'places
        Next i
    Next k
    
    'return values
    CardShuff = TheDeck(DeckTot)
End Function

Public Sub Shuffle()
    Dim nDecks As Long
    
    'nDecks = InputBox("Enter number of decks (1 to 8)", "Number of Decks")
    'nDecks = CLng(UserForm1.TextBox1.Text)
    nDecks = 2
    doInitialize nDecks
    ReDim TheDeck(52 * nDecks)
    DeckTot = CInt(52 * nDecks)
    InitDeck
    'TimesToShuffle = InputBox("Input number of times to shuffle", "Number of Shuffles")
    'TimesToShuffle = CInt(UserForm1.TextBox2.Text)
    TimesToShuffle = 3
    
    TheDeck(DeckTot) = CardShuff(DeckTot, TimesToShuffle)
    
    Dim n As Integer
    Dim rank As Integer
    
    Worksheets("Sheet1").Activate
    Worksheets("Sheet1").Range("A1:A416").ClearContents
    For n = 0 To DeckTot - 1
        If TheDeck(n) < 36 Then
            Worksheets("Sheet1").Cells(n + 1, 1).Value = TheDeck(n) \ 4 + 1
        Else
            Worksheets("Sheet1").Cells(n + 1, 1).Value = 10
        End If
    Next n
    Stop
    'Let n=(cards-1), where cards=52*decks
    'If cards=1, reference top card
    'If cards=2, reference second card
    'If cards=n, reference nth card
    'At this point TheDeck(n) holds these cards
    'If TheDeck(n)=(0 to 3) the card is an ace
    'If TheDeck(n)=(4 to 7) the card is an two
    'If TheDeck(n)=(8 to 11) the card is an three
    'If TheDeck(n)=(12 to 15) the card is an four
    'If TheDeck(n)=(16 to 19) the card is an five
    'If TheDeck(n)=(20 to 23) the card is an six
    'If TheDeck(n)=(24 to 27) the card is an seven
    'If TheDeck(n)=(28 to 31) the card is an eight
    'If TheDeck(n)=(32 to 35) the card is an nine
    'If TheDeck(n)=(36 to 51) the card is an ten
End Sub
k_c
 

k_c

Well-Known Member
sagefr0g said:
appreciate it very much k_c .
still getting a syntax error. uhmm and Visual Basic For Applications is checked.
not to worry i'm sure we'll get it. sure do appreciate it again. thanks much.
Your version of Excel is probably newer than mine. Mine is 9.0. Maybe if you try Tools/References and check Microsoft Excel 9.0 Object Library, it will work. Hopefully it will be there. Also there is a checkbox for Microsoft Office 9.0 Object Library on my machine.

k_c
 

Canceler

Well-Known Member
It's always fun to read somebody else's code. The Input Boxes worked fine for me, until k_c commented them out in the latest version of his program. (I'm using Excel 2007, which appears to be version 12.0.)
 
Top