Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This program uses three-coin approach to consult the IChing and returns the hexagram (see https://harrywang.me/iching for more details)
def consult_oracle():
import random
hex = ''
for i in range(6): # toss 6 times to get the hex
toss = 0
line = 0
for i in range(3): # each toss with three coins to generate one line
x = random.randint(0, 1) # 0 head (counts as 3) 1 tail (counts as 2)
if x == 0:
toss += 3
else:
toss += 2
if toss == 6 or toss == 7:
line = 1
else: # 8 or 9
line = 2
#print(toss, line)
# combine 6 lines to get the hexagram
hex += str(line)
return int(hex)
def find_hex(json_object, hex):
for dict in json_object:
if dict['hexagram'] == hex:
return dict['name']
# https://harrywang.me/iching
def ask_iching():
data = [{'name': 'The Creative / 乾', 'hexagram': 111111}, {'name': 'The Receptive / 坤', 'hexagram': 222222}, {'name': 'Difficulty at the Beginning / 屯', 'hexagram': 212221}, {'name': 'Youthful Folly / 蒙', 'hexagram': 122212}, {'name': 'Waiting (Nourishment) / 需', 'hexagram': 12111}, {'name': 'Conflict / 讼', 'hexagram': 111212}, {'name': 'The Army / 师', 'hexagram': 222212}, {'name': 'Holding Together [union] / 比', 'hexagram': 212222}, {'name': 'The Taming Power of the Small / 小畜', 'hexagram': 112111}, {'name': 'Treading [conduct] / 履', 'hexagram': 111211}, {'name': 'Peace / 泰', 'hexagram': 222111}, {'name': 'Standstill [Stagnation] / 否', 'hexagram': 111222}, {'name': 'Fellowship with Men / 同人', 'hexagram': 111121}, {'name': 'Possession in Great Measure / 大有', 'hexagram': 121111}, {'name': 'Modesty / 谦', 'hexagram': 222122}, {'name': 'Enthusiasm / 豫', 'hexagram': 221222}, {'name': 'Following / 随', 'hexagram': 211221}, {'name': 'Work on what has been spoiled [ Decay ] / 蛊', 'hexagram': 122112}, {'name': 'Approach / 临', 'hexagram': 222211}, {'name': 'Contemplation (View) / 观', 'hexagram': 112222}, {'name': 'Biting Through / 噬嗑', 'hexagram': 121221}, {'name': 'Grace / 贲', 'hexagram': 122121}, {'name': 'Splitting Apart / 剥', 'hexagram': 122222}, {'name': 'Return (The Turning Point) / 复', 'hexagram': 222221}, {'name': 'Innocence (The Unexpected) / 无妄', 'hexagram': 111221}, {'name': 'The Taming Power of the Great / 大畜', 'hexagram': 122111}, {'name': 'Comers of the Mouth (Providing Nourishment) / 颐', 'hexagram': 122221}, {'name': 'Preponderance of the Great / 大过', 'hexagram': 211112}, {'name': 'The Abysmal (Water) / 坎', 'hexagram': 212212}, {'name': 'The Clinging, Fire / 离', 'hexagram': 121121}, {'name': 'Influence (Wooing) / 咸', 'hexagram': 211122}, {'name': 'Duration / 恒', 'hexagram': 221112}, {'name': 'Retreat / 遁', 'hexagram': 111122}, {'name': 'The Power of the Great / 大壮', 'hexagram': 221111}, {'name': 'Progress / 晋', 'hexagram': 121222}, {'name': 'Darkening of the light / 明夷', 'hexagram': 222121}, {'name': 'The Family [The Clan] / 家人', 'hexagram': 112121}, {'name': 'Opposition / 睽', 'hexagram': 121211}, {'name': 'Obstruction / 蹇', 'hexagram': 212122}, {'name': 'Deliverance / 解', 'hexagram': 221212}, {'name': 'Decrease / 损', 'hexagram': 122211}, {'name': 'Increase / 益', 'hexagram': 112221}, {'name': 'Break-through (Resoluteness) / 夬', 'hexagram': 211111}, {'name': 'Coming to Meet / 姤', 'hexagram': 111112}, {'name': 'Gathering Together [Massing] / 萃', 'hexagram': 211222}, {'name': 'Pushing Upward / 升', 'hexagram': 222112}, {'name': 'Oppression (Exhaustion) / 困', 'hexagram': 211212}, {'name': 'The Well / 井', 'hexagram': 212112}, {'name': 'Revolution (Molting) / 革', 'hexagram': 211121}, {'name': 'The Caldron / 鼎', 'hexagram': 121112}, {'name': 'The Arousing (Shock, Thunder) / 震', 'hexagram': 221221}, {'name': 'Keeping Still, Mountain / 艮', 'hexagram': 122122}, {'name': 'Development (Gradual Progress) / 渐', 'hexagram': 112122}, {'name': 'The Marrying Maiden / 归妹', 'hexagram': 221211}, {'name': 'Abundance [Fullness] / 丰收', 'hexagram': 221121}, {'name': 'The Wanderer / 旅', 'hexagram': 121122}, {'name': 'The Gentle (The Penetrating, Wind) / 巽', 'hexagram': 112112}, {'name': 'The Joyous, Lake / 兑', 'hexagram': 211211}, {'name': 'Dispersion [Dissolution] / 涣', 'hexagram': 112212}, {'name': 'Limitation / 节', 'hexagram': 212211}, {'name': 'Inner Truth / 中孚', 'hexagram': 112211}, {'name': 'Preponderance of the Small / 小过', 'hexagram': 221122}, {'name': 'After Completion / 既济', 'hexagram': 212121}, {'name': 'Before Completion / 未济', 'hexagram': 121212}]
hex = consult_oracle()
hexagram = find_hex(data, hex)
print(hexagram)
return hexagram # return the name of the hexagram
if __name__ == "__main__":
ask_iching()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment