Thursday, July 31, 2025

Communication without complexity

Last Sunday we talked in church (sacrament meeting). Partly to put off preparation, and partly because it was there, I was playing around with a little web page produced by a program that I wrote many years ago. The program's job is to display a random verse from the Book of Mormon.

The message

Most verses are short enough to display nicely on the screen of my iPhone, and I just pull one down — to cause the page to be reloaded — to see the next random pick. As I was doing this, I was galvanized by a verse that appeared (Alma 29:9) because it was precisely what I needed for the topic of my talk.

The more I reflected on this, the more amazed I became. There are over six thousand verses to pick from, and the code that does it is extremely small. It is also very mechanical. So, I choose to believe that seeing this verse at that time was a kind of message from a higher power, because it was precisely what I needed at that time.

It described my message — the one that I ended up delivering — which is that sometimes a person can be at the right place at the right time with just the right tale to have an impact on the world. It was the story of that one time when I was "an instrument in the hands of God to bring [one] soul to repentance;" and that was "my joy."

How the program works

[Victims of mathophobia can skip this section.]

Since it involves a random number, the odds are very low that the program would pick out any particular verse. The one it picked happens to be number 3,332.

#!/bin/bash
echo "Content-type: text/plain; charset=utf-8"
echo
sed -n `echo $((RANDOM%6604+1))`p ../../bofm/eng/all.txt

As indicated by the first line of the code, this script is to be interpreted by the Bash shell (that has been used —in the same way — in many of the earlier posts on this blog).

The next two lines are just sent back to the browser as-is. Line two lets the browser know that what it receives next is just plain text (as opposed to HTML, say). And, line three (a completely empty line) lets the browser know that that's all we have to say about what will be sent.

All of the work is done in line four, repeated below with some extra spaces and underlining to show four distinct sections.

sed  -n  `echo $((RANDOM%6604+1))`p  ../../bofm/eng/all.txt

It runs the sed program, which is a stream editor, which operates on its input line by line. Its input is the file located at the path given in part four, two directories above the one the program resides in and then in a folder named bofm containing a folder named eng containing the file named all.txt (which contains in order all 6604 verses of the Book of Mormon (one and half million characters)).

Normally this program would print out every line as it was read in. The -n flag tells it not to produce any output unless explicitly requested. 

The next bit of code looks really complicated, but it just gets re-written in stages. Those stages are shown below, one at a time.

`echo $((RANDOM%6604+1))`p
`echo $((23143%6604+1))`p
`echo $((3331+1))`p
`echo 3332`p
3332p

That process is called "evaluation" and happens from the inside-out. First a random number is selected from the range 0–32,767 which here we are assuming was 23,143. Then it is divided by 6,604 and the remainder* (if you have a calculator with a "mod" key, you can try this out), 3331, is retained. Then one is added to that, giving 3,332. Finally the back-tick part becomes just the string "3332" which is concatenated to the "p". So, in this case, it is as if the hard-coded command

sed -n 3332p ../../bofm/eng/all.txt

had been used. That simply asks sed to look in turn at every line in the all.txt file and print out line 3,332 ignoring all the other lines. So, the browser only sees that one line, which is verse 9 of Alma chapter 29.

Each time this program runs, a different random number is chosen, and so a different verse gets shown. Everything in the program remains the same, except that RANDOM is replaced by a different number each time the program runs.

Tying in the title

This was a communication to my mind of a particular verse of scripture that I needed to make a cohesive message for a talk and it came very simply, through a device that I myself had created years earlier. The mystery is that that particular random number was chosen at that particular time. For whatever reason it was, this recent event perfectly mirrored the event in my story in that just the right ingredients all came together at the right time.

Notes

Alma 29:9 says "I know that which the Lord hath commanded me, and I glory in it. I do not glory of myself, but I glory in that which the Lord hath commanded me; yea, and this is my glory, that perhaps I may be an instrument in the hands of God to bring some soul to repentance; and this is my joy."

*Because of the Quotient-Remainder Theorem of arithmetic, whenever you divide an integer by 6,604 you will get a unique quotient and remainder. In our example,  if the integer was 23,143 then the quotient is 3 and the remainder is 3,331. The remainder is also called the modulo. That is, 23143 = 3 x 6604 + 3331

"if the integer was 23,143" Other integers that lead to the same verse are 3331, 9935, 16,539, and 29747, so we don't know and can't tell what the actual random number was, just that it was one of these five possibilities.

Perhaps you will take issue with my claim that "it came very simply" in view of the long explanation of how it works. Keep in mind that line four of the program is almost as simple as a program can be.

1 comment: