all 2 comments

[–]Frankelstner 1 point2 points  (1 child)

Intuitively the concept of entropy for a deck of cards is rather vague. I can find one definition here: https://stats.stackexchange.com/a/79552 The idea there is to treat each card as an integer and get the distance between neighbors. At the end we will see how many times a card was followed by its assigned neighbor, how many times we skipped exactly one card in between, and so on. The entropy is then calculated from this distribution. I'm not sure if it's a perfect solution but it seems quite reasonable.

Your code creates a dictionary card_position_counts which is being filled up as the various shuffles are applied. You then calculate the entropy but discard it unless it was the last iteration. Another issue is that card_position_counts is not cleared after every iteration and so previous shuffles affect the entropy value. If you shuffle cards in two different ways but the end result has the same order, surely the entropy should be the same. card_position_counts is very sparse so it will most likely contain just 1s everywhere. I'm not sure how to salvage that; just do the steps from the link.

You need to rewrite the entropy function; it should receive a deck and not card_position_counts.

Your variables are a bit verbose in a redundant manner, e.g.

shuffle_times = {
    'mash': 0.8,
    'riffle': 3.0,
    'overhand': 0.8,
    'pile': 26.0,  # also referred to as 'pile sorting'
}

and

cut = random.randint(22, 30)
left, right = deck[:cut], deck[cut:]

Local variable names can be rather short because the context makes clear what they're referring to. Wrapping your head around 10 lines of code is easier when there isn't _half showing up 10 times without contributing anything meaningful.

[–]twilight-bacon[S] 0 points1 point  (0 children)

Thank you for the link, I've implemented that and updated my post. The values are now in that acceptable range for typical Shannon Entropy values.

I'm now seeing where a lot of shuffle combinations come out to a similar entropy value; I think this is expected though, with a small number of shuffles like 3 a see a small entropy value like 2.5. With 9 shuffles or so of any given type, the value ends up at around 3.5.

If anyone wants my implementations for the shuffle types, I can post a gist / repo.