David A. Wheeler's Blog

Sun, 12 Mar 2006

Random Quotes and Code - Why You Need a Community

You need a community, not just some dump of posted code, if you want good open source software. I can demonstrate this through my trivial hunt for “random quote” code… so let me tell you my story.

I recently decided that I’d like the front page of my website to show a randomly-selected quote. For security reasons, I avoid using dynamically-run code on my own site, so I needed to use Javascript (ECMAscript) to do this. Easy enough, I thought… I’ll just use Google to find a program that did this, and I searched on “random quotation Javascript”.

But what I found was that a lot of people don’t seem to care about long-term maintenance, or correctness. Codelifter’s sample code by etLux does the job, but also shows the problem. The code has a lot of statements like this:

 Quotation[0] = "Time is of the essence! Comb your hair.";
 Quotation[1] = "Sanity is a golden apple with no shoelaces.";
 ...
Does this work? Sure, but it’s terrible for maintenance. Now you have to write extra code, unnecessarily maintain index numbers, and if you want to delete a quote in the middle, you have to renumber things. Even for tiny tasks like this, maintenance matters over time. I’m going to use this for my personal website, which I plan to have for decades; life is too short to fight hard-to-maintain code over a long time.

Even worse, this and many other examples did a lousy job of picking a random quote. Many sample programs picked the random quote using this kind of code (where Q is the number of quotes):

 var whichQuotation=Math.round(Math.random()*(Q-1));
This actually doesn’t choose the values with equal probability. To see why, walk through the logic if there are only 3 quotes. Math.random returns a value between 0 and 1 (not including 1); if there are 3 quotes, Math.random()*(Q-1) produces a floating point value between 0 and 2 (not including 2). Rounding a value between 0 and 0.5 (not including 0.5) produces 0, between 0.5 and 1.5 (not including 1.5) produces 1, and between 1.5 and 2 produces 2…. which means that the middle quote is far more likely to be selected (it will be selected 50% of the time, instead of the correct 33%). The “round” operation is the wrong operator in this case!

I’m not really interested in picking on the author of this code sample; LOTS of different sample codes do exactly the wrong thing.

The problem seems to be that once some code snippet gets posted, in many places there’s no mechanism to discuss the code or to propose an improvement. In other words, there’s no community. I noticed these problems immediately in several samples I saw, yet there was no obvious way for me to do anything about it.

In the end, I ended up writing my own code. For your amusement, here it is. Perhaps there needs to be a “trivial SourceForge” for taking tiny fragments like this and allowing community improvement.

First, I put this in the head section of my HTML:

<script language="JavaScript">
 // Code by David A. Wheeler; this trivial ECMAscript is public domain:
  var quotations = new Array(
    "Quote1",
    "Quote2",
    "Quote3"
  );
 var my_pick = Math.floor(quotations.length*Math.random());
 var random_quote = "Your random quote: <i>" + quotations[my_pick] + "</i>";
</script>

I then put this in the body section of my HTML:

<script language="JavaScript">
 document.write(random_quote)
</script>

I intentionally didn’t include some defensive measures against bad software libraries. Unfortunately, many software libraries are terrible, and that certainly includes many random number generators. For example, Math.random() isn’t supposed to return 1, only values less than that… but returning an (incorrect) 1 isn’t an unknown defect, and that would cause an out-of-bounds error. Also, many implementations of random() are notoriously bad; they often have trivially tiny cycles, or fail even trivial randomness tests. I would put defensive measures in software designed to be highly reliable or secure (for example, I might re-implement the random function to be sure I got a reasonable one). But in this case, I thought it’d be better to just rely on the libraries… if the results are terrible, then users might complain to the library implementors. And if the library implementors fix their implementations, it helps everyone.

I donate the above snippet to the public domain. It’s not clear at all that it can be copyrighted, actually; it’s far too trivial. But it’s still useful to have such snippets, and I hope that someone will organize a community for sharing and maintaining trivial snippets like this.

path: /oss | Current Weblog | permanent link to this entry

In memorium: William Alson (“Al”) Wheeler

Picture of William Alson (Al) Wheeler William Alson (“Al”) Wheeler (2 March 1916 – 28 February 2006) was my grandfather and a wonderful, godly man. He recently went to be with the Lord, and I will miss him very much until my time comes. Many of my own traits (love of music, math, science fiction, science, and learning in general) are easily traced back to him. He loved jokes and humor; he laughed often, and his eyes often twinkled.

He demonstrated his extraordinary character throughout his life; a few anecdotes will have to suffice. His love of learning was extraordinary; in his 80s he started learning koine Greek, and near the time of his death he was reading the 984-page “Chaos and Fractals: New Frontiers of Science” (a book full of mathematical concepts). He dedicated his life to serving others; he was a music minister for over 45 years. He prayed, and prayed often, for his family and friends. When he last moved to Pennsylvania, he donated his mechanic’s tools to the Smithsonian, which had been hunting for the kind of tools he had. And even in death he served others; rather than having his body be buried, he donated his body for medical research. I am honored that I can count myself as one of his grandchildren. He did not leave riches behind; he left behind something much greater.

Below is a short biography of his life, as printed at his memorial service on March 4, 2006. May we all strive to have such a positive biography. “Children’s children are a crown to the aged, and parents are the pride of their children.” (Proverbs 17:6, (NIV)) (New International Version)


Al was born in the small town of Pottsville, PA. He grew up in Oxford, PA and graduated in 1933 from Oxford High School. He worked a couple years with the Wheeler and Sautelle and then the Wheeler and Almond Circuses. In 1935 he moved to Reading, PA, and entered the Wyomissing Polytechnic Institute to become a machinist. He met his future wife, Mary Clouse, in 1938 at a YWCA sponsored dance. It was during this time that Al taught himself to play tennis and the clarinet. He came from a musical family and had begun singing with the Choral Society in Reading. He began working at the Textile Machineworks in Wyomissing, but found a job in early 1940 with the Federal Government at the Naval Gun Factory in Washington, DC. He married Mary in June 1940. While living in southeast Washington, they had three children: Bill, Ray and Joyce. In 1946, after a few interim jobs, he began working for the Bureau of Standards followed by the Naval Research Laboratory. He moved to the Bureau of Naval Weapons in 1957. He moved his family to Maryland in 1958. In 1962 he made his last career move to the Naval Air Systems Command and retired from there in 1974. Throughout this time he should have received a chauffeur’s license since he performed that duty extensively. Mary went home to be with the Lord in 1996. In 1999 he moved to Perkiomenville, PA to live with his daughter, son-in-law Phil, and grandson Bryan.

After moving to Washington the war initially kept him out of church activities. Mary had started attending Fountain Memorial Baptist Church. After the war, Al started attending also. They professed Christ as Lord and Savior and were baptized together. In 1950, Al started directing the Junior Choir, grades 4 thru 6. In 1953 he became the Music Director at Fountain Memorial and spent the next 45 years directing music in 6 different churches. He loved his music and his collection of choir music, mini orchestral scores, records, reel-to-reel tapes, cassette tapes, and CDs attest to it. His primary instrument was the clarinet, but he had obtained and played saxophone, flute, and trombone and, at one point, two synthesizers.

Although work and family responsibilities cut down on his tennis activities, he never gave them up completely. After retirement he taught tennis part time for the Maryland Department of Recreation and was regularly playing with his friends until moving back to PA in 1999.

He loved science fiction and one of his favorite pastimes was solving the “Word Power” article in the Reader’s Digest. He rarely missed those words.

Al had a wonderful life and was adored by all his family. He is survived by three children, five grandchildren, and 7 great-grandchildren.

path: /misc | Current Weblog | permanent link to this entry