Linear Feedback Shift Registers and Hexagram Generation

Sometimes things just click. I recently started to practise uxn again, after finally having some headspace freed up. Some exercises later I guessed I should probably start work on a small project if I want to advance. There is only so much to be learned from solving pseudocode exercises in a concatenative programming language. My choice fell on an I Ching Hexagram generator, since these already fascinated me when I worked on Ostrakinda. Basically, a hexagram is made from six lines, which are either whole or broken. Two to the power of six is sixty-four, meaning there are sixty-four different hexagrams. There is a variety of different methods for I Ching divination, and some of them involve coins, which is the reason why I got interested in this type of cleromancy in the first place.

At first I thought that it is as simple as throwing a coin and one side means a whole line while the other side would indicate a broken line. But why make it simple if you can make it esoteric? Instead, the hexagram divination method must result in a number between six and nine. The number indicates if a line is ‘young’ (not changing) or ‘old’ (changing). Most of them are ‘young’, but if you have some of the rare ‘old’ lines, you get a hexagram with variants where you have to read all the possible combinations where the ‘old’ line is broken and whole.

Coins Binary Decimal Line
H T T T 1000 8 ——-
H T T H 1001 9  
H T H T 1010 10  
H T H H 1011 11  

One of several tables taken from the explanations of the four coins aka four bits method.

Either way, I needed to implement a random number generator (RNG) in uxn. I could have used the one from Devine, but since I wanted to practise uxntal and learn a thing or two, I needed to understand the underlying logic and maths that would create a pseudo-random number. In the process of researching some easy documentation of it (random number generator for dummies), I encountered the linear-feedback shift register (LFSR), which is a type of RNG. But instead of using normal-people maths, it works with computational operations, XOR and bit shifting to be precise. And that is the moment when it clicked. Because the Wikipedia page on I Ching divination methods actually featured a version that works with four bits! So what’s better than having a four-bit divination method and an LFSR? Match made in heaven! Some moments later I had implemented the algorithm in yarromancy and I believe it is working out, although there is a lot of room for optimisation and improvement.

A 16-bit Fibonacci LFSR. A 16-bit Fibonacci LFSR.

But that is part of the journey, isn’t it?