I have written a fair amount of code during my career. Not much of it has been widely used, which doesn't bother me, because most of it was written for my own use or for examples in text books and course notes. The one little snippet that does seem to have been used quite a bit is best known for its badness. This web page is my apologia for it: a story with a moral.
In 1977, I was working on a book, Programming in Pascal, ("PP") that would eventually be published by Addison-Wesley in 1978, with later editions in 1980 and 1984. One of my goals was to provide many examples program, because I believe that people learn more easily from examples than from abstractions. The programs had to be short so that the book would not be excessively long, but I also wanted to make them interesting. In particular, I decided to use a discrete event simulation as an example, and this required a random number generator (RNG).
Since I was writing an introductory text book about programming rather than an academic tome about numerical analysis, I did not spend a lot of time designing an RNG. I consulted Donald Knuth's Art of Computer Programming, Volume 2, Seminumerical Algorithms (Addison-Wesley, 1969) and discovered that Knuth recommended Linear Congruential Generators and gave some simple rules for choosing parameters (pages 9-24). After a few simple experiments, I settled on this RNG (PP, second ed., page 137):
procedure random (var randint: integer); begin random := randint; randint := (25173 * randint + 13849) mod 65536 end;
The book, in various editions, sold very well and became known, in the mid-1980s, as a "standard" text book. When people wanted a RNG, the easiest one to find was often mine. And so it came to pass that the five lines that I had written so thoughtlessly became used for serious applications. For example, Martin Sullivan needed a RNG and "eventually found a very cribable example in a standard programming textbook". His client (a microscopist) became suspicious of the code and decided to test the RNG and "instead of a uniformity of grey that he should have gotten, he saw a pretty wavy pattern". Oops.
Other researchers noticed that "many generators have been written, most of them have domonstrably non-random charcterisits, and some are embarrassingly bad" (Park, S. K. and K. W. Miller. Random number generators: good ones are hard to find. Communications of the ACM, 31(10), October 1988, pages 1192-1201). They mention my generator, noting that it has a full period of length 65536 and can be easily implemented with 32-bit words. However, it is "unsatisfactory for scientific applications" and the researchers were "not aware of any published T2-test results". (I agree with both points: I didn't intend it to be used for scientific applications and I didn't test it carefully.) However, "most of the otherr mixed generators found in computer science texts are even less satisfactory than Grogono's generator". Well, that's a relief!
The years passed, and I came across one of Clifford Pickover's many books (Keys to Infinity. Wiley, 1995.) In Chapter 31, Pickover describes a simple but quite effective technique for testing RNGs visually. The idea is to generate random numbers in groups of three, and to use each group to plot a point in spherical coordinates. If the RNG is good, the points will form a solid sphere. If not, patterns will appear.
When it is used with good RNGs, the results of the Pickover Test are rather boring: it just draws spheres. The test is much more effective when it is used with a bad RNG, because it produces pretty pictures. In particular, the Grogono Generator is so bad that it generates quite delightful patterns. Here are some examples, starting with three from my generator.
The Fibonacci numbers modulo N, where N is a large number, have been used as RNGs. The Pickover test shows that they are not very random.
Pickover spent a few minutes designing a generator of his own: x' = y - floor(y), where y = k log x. For k=100, this RNG is quite good. When k is small, non-randomness rears its pretty head.
Let's conclude with a good RNG: the Mersenne Twister.
I mentioned a moral somewhere back there. Here it is: don't asssume that anything you write, however trivial, may not one day escape into the wild and do damage there.