5D Library › Dream Motif Bipartite Graphs, Deterministic Oracle Decks and Subconscious Prior Activation · 中文
Chapter 3. A Deterministic 54-Card Deck: SHA-256 and the Fisher-Yates Shuffle
Why make an oracle deck deterministic
An online card reading has to come from somewhere. If it comes from the server’s random number generator, nobody can check afterwards what was drawn or whether the draw was redone. If it comes from a hash of the user, the date and the question, the same inputs always give the same cards, anyone with the inputs can recompute them, and the operator cannot quietly choose a “better” card. The deck here has 54 cards: four suits of 13 ranks (standing for four dimensions of a situation) and two jokers.
The construction has three parts. The seed is the SHA-256 digest (National Institute of Standards and Technology 2015) of the user ID, the date and a digest of the question text. A stream of 32-bit words comes from hashing the seed with a counter. And the deck is shuffled by the Fisher-Yates algorithm in Durstenfeld’s form (Durstenfeld 1964; Knuth 1997): for each position from the last down to the second, swap it with a position chosen uniformly at random from those not yet fixed.
Calculation
# Chapter 3. p05: A deterministic 54-card oracle shuffle from SHA-256.
# Deck: four suits F, W, A, E of 13 ranks, plus two jokers JK1, JK2.
# Seed = SHA-256(user | date | SHA-256(question)); a counter-mode stream
# SHA-256(seed || i) gives 32-bit words; an index below n is drawn by
# rejection (words at or above the largest multiple of n are discarded),
# and the deck is shuffled by Fisher-Yates (Durstenfeld). The top three
# cards are the past / present / way-out spread. Uniformity is checked
# over 54,000 user IDs with a chi-square test (Wilson-Hilferty p-value).
import hashlib
import math
DECK = ["{}{:02d}".format(s, r) for s in "FWAE" for r in range(1, 14)]
DECK += ["JK1", "JK2"]
def seed_of(user, date, question):
q = hashlib.sha256(question.encode("utf-8")).hexdigest()
return hashlib.sha256("{}|{}|{}".format(user, date, q)
.encode("utf-8")).digest()
class Stream:
def __init__(self, seed):
self.seed, self.i, self.buf = seed, 0, b""
def word(self):
if not self.buf:
self.buf = hashlib.sha256(
self.seed + self.i.to_bytes(4, "big")).digest()
self.i += 1
w, self.buf = int.from_bytes(self.buf[:4], "big"), self.buf[4:]
return w
def below(self, n):
limit = (2 ** 32 // n) * n
while True:
w = self.word()
if w < limit:
return w % n
def shuffle(seed):
deck, st = list(DECK), Stream(seed)
for i in range(len(deck) - 1, 0, -1):
j = st.below(i + 1)
deck[i], deck[j] = deck[j], deck[i]
return deck
def p_chi2(x, df):
z = ((x / df) ** (1 / 3) - (1 - 2 / (9 * df))) / math.sqrt(2 / (9 * df))
return 0.5 * math.erfc(z / math.sqrt(2))
if __name__ == "__main__":
s = seed_of("u001", "2026-11-09", "Should I launch the course now?")
d = shuffle(s)
print(" seed {}... spread: {}".format(s.hex()[:16], " ".join(d[:3])))
print(" same inputs again give the same spread: {}".format(
shuffle(seed_of("u001", "2026-11-09",
"Should I launch the course now?"))[:3] == d[:3]))
print(" ordered three-card spreads: 54 x 53 x 52 = {:,}".format(
54 * 53 * 52))
n = 54000
top = dict.fromkeys(DECK, 0)
jpos = [0] * 54
for u in range(n):
d = shuffle(seed_of("u{:05d}".format(u), "2026-11-09",
"Should I launch the course now?"))
top[d[0]] += 1
jpos[d.index("JK1")] += 1
e = n / 54
print(" {:,} users, expected count per card {:,.0f}".format(n, e))
for name, obs in (("top card", list(top.values())),
("position of JK1", jpos)):
x = sum((o - e) ** 2 / e for o in obs)
print(" {:15s}: chi-square {:.1f} on 53 df, p = {:.3f}, "
"counts {}-{}".format(name, x, p_chi2(x, 53), min(obs),
max(obs)))
# seed 6b33b8f955f4aaf1... spread: F13 W02 E08
# same inputs again give the same spread: True
# ordered three-card spreads: 54 x 53 x 52 = 148,824
# 54,000 users, expected count per card 1,000
# top card : chi-square 66.8 on 53 df, p = 0.096, counts 907-1083
# position of JK1: chi-square 57.5 on 53 df, p = 0.312, counts 924-1083The demonstration user gets F13, W02, E08 for the past, the present and the way out, and gets the same three cards every time the same question is asked on the same day. There are 54 x 53 x 52 = 148,824 ordered three-card spreads. Over 54,000 user IDs, the top card’s counts range from 907 to 1083 around the expected 1,000, with a chi-square of 66.8 on 53 degrees of freedom (p = 0.096); the position of one joker gives 57.5 (p = 0.312). Neither departs from uniformity.
Three ways to get it wrong
The uniformity above depends on two details that are easy to get wrong: how a hash word becomes an index, and which positions a swap may reach.
Calculation
# Chapter 3. p06: Three ways to get a "random" shuffle wrong.
# (a) taking one hash byte modulo 54 (5/256 or 4/256, odds 1.25);
# (b) a 32-bit word modulo 54; (c) the naive shuffle that swaps every
# position with ANY position (n**n equally likely paths spread over n!
# orders); (d) Sattolo's slip of drawing j below i instead of up to i,
# which only makes single cycles, so no card can stay where it was.
# All counts are exact.
from fractions import Fraction
from itertools import product
from math import factorial
def mod_bias(bits, n):
m = 2 ** bits
q, r = divmod(m, n)
hi, lo = Fraction(q + 1, m), Fraction(q, m)
tv = (r * abs(hi - Fraction(1, n)) +
(n - r) * abs(lo - Fraction(1, n))) / 2 if r else Fraction(0)
return (hi / lo if r else Fraction(1)), tv, r
def naive_counts(n):
cnt = {}
for js in product(range(n), repeat=n):
a = list(range(n))
for i, j in enumerate(js):
a[i], a[j] = a[j], a[i]
cnt[tuple(a)] = cnt.get(tuple(a), 0) + 1
return cnt
def sattolo_orders(n):
seen = set()
def rec(a, i):
if i == 0:
seen.add(tuple(a))
return
for j in range(i):
b = list(a)
b[i], b[j] = b[j], b[i]
rec(b, i - 1)
rec(list(range(n)), n - 1)
return seen
if __name__ == "__main__":
for bits in (8, 32):
ratio, tv, r = mod_bias(bits, 54)
print(" {:2d}-bit word mod 54: {} favoured cards, odds {:.10f}, "
"TV {:.3g}".format(bits, r, float(ratio), float(tv)))
print(" naive swap-with-any shuffle:")
for n in range(3, 8):
c = naive_counts(n)
lo, hi = min(c.values()), max(c.values())
print(" n={}: {:,} paths over {:,} orders, count {}-{}, "
"ratio {:.3f}".format(n, n ** n, factorial(n), lo, hi,
hi / lo))
for n in (4, 5, 6):
s = sattolo_orders(n)
fixed = sum(1 for p in s if any(p[i] == i for i in range(n)))
print(" Sattolo slip, n={}: {} of {} orders, {} with a card "
"left in place".format(n, len(s), factorial(n), fixed))
print(" for 54 cards the slip reaches 1/54 of orders: {:.4f}".format(
factorial(53) / factorial(54)))
# 8-bit word mod 54: 40 favoured cards, odds 1.2500000000, TV 0.0405
# 32-bit word mod 54: 22 favoured cards, odds 1.0000000126, TV 3.04e-09
# naive swap-with-any shuffle:
# n=3: 27 paths over 6 orders, count 4-5, ratio 1.250
# n=4: 256 paths over 24 orders, count 8-15, ratio 1.875
# n=5: 3,125 paths over 120 orders, count 16-47, ratio 2.938
# n=6: 46,656 paths over 720 orders, count 32-159, ratio 4.969
# n=7: 823,543 paths over 5,040 orders, count 64-543, ratio 8.484
# Sattolo slip, n=4: 6 of 24 orders, 0 with a card left in place
# Sattolo slip, n=5: 24 of 120 orders, 0 with a card left in place
# Sattolo slip, n=6: 120 of 720 orders, 0 with a card left in place
# for 54 cards the slip reaches 1/54 of orders: 0.0185Taking one hash byte modulo 54 makes 40 cards five chances in 256 and the other 14 only four: an odds ratio of 1.25 and a total variation distance of 0.0405 from fair. A 32-bit word reduces the odds ratio to 1.0000000126, negligible for any practical purpose; rejecting the few words above the largest multiple of 54 removes it entirely, which is what p05 does. The naive shuffle that swaps every position with any position spreads equally likely paths over orders, which cannot be even: with 4 cards, some orders come up 15 times out of 256 and others 8; with 7 cards the most likely order is 8.484 times as likely as the least. Sattolo’s slip of drawing the partner strictly below the current position produces only single cycles (Sattolo 1986): with 6 cards just 120 of 720 orders are reachable and no card ever stays in its place; with 54 cards only 0.0185 of all orders can occur.
Note
Determinism is not meaning. A hash makes a reading reproducible and checkable; it does not make the reading say anything about the question. The cards follow from the bytes of the question, not from its sense, as the next chapter shows.
Summary
A seed hashed from user, date and question, a counter-mode SHA-256 stream, rejection sampling and the Durstenfeld shuffle give a reproducible deck whose top card passes a chi-square test over 54,000 users. Taking a byte modulo 54, swapping with any position, or drawing strictly below the current position all bias the deck, the last two badly.
References
Fan, Y. (2026). Dream Motif Bipartite Graphs, Deterministic Oracle Decks and Subconscious Prior Activation: REM Semantic Spreading, SHA-256 Fisher-Yates Permutations and Retroactive Coincidence Rates [梦境意象二分图、确定性哈希牌卡与潜意识先验激活] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090233 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).