Skip to content

Instantly share code, notes, and snippets.

@odudex
Last active May 3, 2024 19:53
Show Gist options
  • Save odudex/37b3366e7171b27fda315ae47096a36b to your computer and use it in GitHub Desktop.
Save odudex/37b3366e7171b27fda315ae47096a36b to your computer and use it in GitHub Desktop.
import random
def pattern_detection(rolls, num_sides):
"""Calculate Shannon's entropy of roll derivatives to detect arithmetic progression patterns.
Args:
rolls (list of int): List of dice rolls.
num_sides (int): Number of sides on the dice.
Returns:
int: A percentage representing how much the sequence deviates from maximum entropy (0% is random, 100% is non-random).
"""
import math
if len(rolls) < 2:
return 0 # Not enough data to analyze
# Calculate derivatives
derivatives = [int(rolls[i]) - int(rolls[i - 1]) for i in range(1, len(rolls))]
min_derivative, max_derivative = -num_sides + 1, num_sides - 1
derivative_range = max_derivative - min_derivative + 1
derivative_counts = [0] * derivative_range
# Count each derivative
for derivative in derivatives:
index = derivative - min_derivative
derivative_counts[index] += 1
# Calculate entropy
total_derivatives = len(derivatives)
derivative_entropy = sum(-p / total_derivatives * math.log2(p / total_derivatives) for p in derivative_counts if p > 0)
# Normalize entropy
max_entropy = math.log2(derivative_range)
normalized_entropy = (max_entropy - derivative_entropy) / max_entropy * 100
return int(normalized_entropy)
# Test
d6_pattern_rolls = [
"12345612345612345612345612345612345612345612345612",
"11111111122222222333333334444444455555555566666666",
"12345665432112345665432112345665432112345665432112",
"24624624624624624624624624624624624624624624624624",
"36363636363636363636363636363636363636363636363636",
"12121212121212121212121212121212121212121212121212",
"65432165432165432165432165432165432165432165432165",
"12365412365412365412365412365412365412365412365412",
"12654312654312654312654312654312654312654312654312",
"24612345612345612345612345612345612345612345612345",
"32165432165432165432165432165432165432165432165432",
"65432165432165432165432165432165432165432165432165",
"63524163524163524163524163524163524163524163524163",
"36251436251436251436251436251436251436251436251436",
"14253614253614253614253614253614253614253614253614",
"41526341526341526341526341526341526341526341526341",
]
for rolls in d6_pattern_rolls:
print(rolls+":", str(pattern_detection(([int(digit) for digit in rolls]), 6))+"%")
print("\n")
for _ in range(5):
rolls_d6_random = [random.randint(1, 6) for _ in range(50)]
str_list = map(str, rolls_d6_random) # Convert each integer to a string
joined_string = ''.join(str_list)
print(joined_string + ":", str(pattern_detection(rolls_d6_random, 6)) + "%")
print("\n")
rolls_d20_set = "1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-1-2-3-4-5-6-7-8-9-10"
print("D20 pattern rolls:", str(pattern_detection(rolls_d20_set.split("-"), 20)) + "%")
print("\n")
for _ in range(5):
rolls_d20_random = [random.randint(1, 20) for _ in range(30)]
print("D20 random rolls:", str(pattern_detection(rolls_d20_random, 20)) + "%")
@jdlcdl
Copy link

jdlcdl commented May 3, 2024

Attempting to roll fingers in different patterns on the number pad, tried these d6_pattern_rolls:

    "12365412365412365412365412365412365412365412365412",
    "12654312654312654312654312654312654312654312654312",
    "24612345612345612345612345612345612345612345612345",
    "32165432165432165432165432165432165432165432165432",
    "65432165432165432165432165432165432165432165432165",
    "63524163524163524163524163524163524163524163524163",
    "36251436251436251436251436251436251436251436251436",
    "14253614253614253614253614253614253614253614253614",
    "41526341526341526341526341526341526341526341526341",
    "61254361254361254361254361254361254361254361254361",
    "12342345345645615612612312342345345645615612612312",
    "12345665432112345665432112345665432112345665432112",

@odudex
Copy link
Author

odudex commented May 3, 2024

12365412365412365412365412365412365412365412365412: 44%
12654312654312654312654312654312654312654312654312: 47%
24612345612345612345612345612345612345612345612345: 74%
32165432165432165432165432165432165432165432165432: 81%
65432165432165432165432165432165432165432165432165: 81%
63524163524163524163524163524163524163524163524163: 58%
36251436251436251436251436251436251436251436251436: 58%
14253614253614253614253614253614253614253614253614: 58%
41526341526341526341526341526341526341526341526341: 58%

@odudex
Copy link
Author

odudex commented May 3, 2024

Some random rolls:
32666462354354141511214112235332256413344652315424: 12%
23535431656516232345151521545414161262333461354665: 8%
26451155524611165353641533456321125431212652425425: 8%
64221464123546211161332315241212243442122462232646: 11%
64132164126224666315561656651663163464652316353311: 9%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment