# LZ

## Table of Contents

## I Ching

The I Ching is a divination method associated with a book of symbols. This is the `Book of Changes`

. You could think about it as randomly selected blobs of advice from a big book of advice.

Let's make an I Ching simulation step-by-step.

## Hexagrams

Each symbol is a hexagram. A hexagram is a binary array of 6 digits. (Therefore 2^{6}=64 different hexagrams). Each is represented vertically with horizontal lines ether complete (yang) or broken (yin).

䷀ ䷁ ䷂ ䷃ ䷄ ䷅

These are the first 6 hexagrams of the King Wen sequence. This special sequence is not intuitive (from the binary perspective) but is typically how the hexagrams are ordered.

## Hexagram selection

To choose a hexagram some randomization method is needed. It is common to use ether coins or sticks (traditionally yarrow stalks). These two methods yield different probabilities. Here we will use the yarrow stalks method, which is older and more elaborate.

## Yarrow stalks algorithm

There is quite a lot of ceremony (activity which does not effect the result) in this method. It would be simplest to just sample from the probabilities and generate the hexagram like that. Instead we are going to simulate it step-by-step, just for fun.

### Process

We generate two hexagrams in parallel. We go line by line from the bottom to the top. Since we generate two at the same time, each "line" can have four different states:

- (6), -x-, broken, changing
- (7), —, unbroken, not changing
- (8), - -, broken, not changing
- (9), -o-, unbroken, changing

Broken refers to the line in the first hexagram, changing refers to whether that bit is flipped in the second hexagram. We will refer to each state by its number. These numbers tie into the hexagram generation process.

There are multiple versions of this method. I've chosen what appeared to be the most common one without some prefix such as "modified" or "simplified" from a brief web search.

### To generate a hexagram:

We are going to make various piles of stalks. I will refer to these with single character names (`l`

, `r`

, `d`

, etc). We also want to recall intermediary results that go into the final result. These will be referred to as `result-a`

, `result-b`

, `result-c`

. For each hexagram we need to generate 6 lines. Each line is attained from the sum of three results.

- Start with 50 stalks in the initial pile
`i`

- Remove one stalk (place it in pile
`x`

). This one will not be used for generating this hexagram. We are left with 49. - Divide
`i`

into two approximately equal piles. We will call these piles`l`

(left) and`r`

(right). - Take one stalk from
`r`

. We will store this in pile of a single stalk`s`

. - Draw from
`l`

in groups of 4, placing the groups of 4 into pile`p`

, until there are 4 or fewer remaining in`l`

. - Draw from
`r`

in groups of 4, placing the groups of 4 into`p`

, until there are 4 or fewer remaining in`r`

. - Sum the count of
`l`

,`r`

and`s`

. If this sum is 5 set`result-a`

to 3, if the sum is 9 then set`result-a`

to 2. These are the only possible sums if the method is followed correctly. - Discard the contents of
`l`

,`r`

and`s`

into discard pile`d`

. - Move the contents of
`p`

back into the initial pile`i`

Repeat steps 3 to 6. - Sum the new contents of
`l`

,`r`

and`s`

. If this sum is 8 set`result-b`

to 2, if the sum is 4 then set`result-b`

to 3. These are the only possible sums if the method is followed correctly. - Discard the contents of
`l`

,`r`

and`s`

into`d`

. - Move the contents of
`p`

back into the initial pile`i`

Repeat steps 3 to 6. - Sum the new contents of
`l`

,`r`

and`s`

. If this sum is 8 set`result-c`

to 2, if the sum is 4 then set`result-c`

to 3. These are the only possible sums if the method is followed correctly. - Sum the values of
`result-a`

,`result-b`

and`result-c`

. This will be ether 6, 7, 8 or 9. This is line's value. - Gather all the stalks from all piles apart from
`x`

(the one removed in step 2). Repeat steps 3 to 14 for each of the remaining 5 lines (ascending).

### Implementation Notes

The algorithm explained above is implemented here in ClojureScript using Scittle to run it in the browser. The code is editable do feel free to tweak it and play around with it. Pseudo-randomness is added at the approx half splitting point in step 3. To make this somewhat realistic we sample from a normal distribution using a Box-Muller transform, and then round to the nearest integer to create an offset to the split.

We use the line generation process and record this as a 6 digit array of 1s and 0s representing the lines. This is converted into an decimal integer and then mapped to its King Wen number. The King Wen number can then be used to get the Unicode symbol and as a reference to look up the symbol's description elsewhere.

The sole purpose of the code below is to represent a common version of the yarrow stalks algorithm as accurately as reasonable in a digital way.

...

## Meanings

The general idea seems to be the transition from the first to the second hexagram. (Book of Changes, remember) You can look up the symbols and contemplate their interpretations here:

cba to scrape the symbol names / interpretations to put inline here right now. Maybe will come back and do that later… xx