oorandom

A minimalistic pseudorandom number generator in Rust. For those times when the rand crate is just too good and you want something a bit dumber.

More specifically, it implements ONE prng, which is probably a permuted congruential generator (PCG) but may change without warning if something better comes along. It will give you u32 or u64, or floating-point equivalents. Anything else is gravy.

Basically the same as https://github.com/Lokathor/randomize. Except MINE, not HIS! HAH! Actually, a lot of the code came from randomize and I wouldn't have done this if I didn't know of that library. So if there are any bugs, I copied them from randomize.

A brief history of non-cryptographic pseudorandom numbers

The usefulness of random numbers has been known for a long, long time by people who also knew how to use slide rules. If you wanted to do some math without the bother of coming up with all that pesky input data from the real world, you might as well just use any ol' number, as long as it didn't heck up your math too bad. So in the first half of the 20th century you had little old ladies named Edith spinning roulette wheels or pulling bingo balls and writing the results down, which got assembled into giant tomes and published so that engineering schools could buy them and have giant tomes sitting on their shelves. Anyone who wanted some meaningless numbers could pull the tome down, flip it open to a presumably-random page, and life was good.

In late 1940's computers were invented, but they were far too big and expensive to be put on the task of INTENTIONALLY generating nonsense, and things carried on as before. If you needed random numbers in a computer, you just got a pretty young lady named Marie to transcribe part of the book to punch cards for you.

Around the early 60's computers got fast enough that Edith and Marie couldn't keep up with them, so they got downsized and replaced with more computers. People came up with Linear Congruential Generators (LCG's), which could generate lots of numbers numbers that weren't really random, but sure looked random, and it worked on computers that even a second-rate university could afford. So, the problem was solved.

At some unknown point, presumably sometime in the 60's or 70's, someone seems to have invented Linear Feedback Shift Registers (LFSR's), which made random-looking numbers and were really easy to implement in hardware instead of needing complicated things like binary multipliers. The random-looking numbers were good enough for hardware people, so they started using LFSR's whenever they needed to and never looked back.

By the late 60's people who knew how to use slide rules had realized that using numbers that only looked random could really heck up their math pretty bad, and one of the most common LCG implmentations, RANDU, was actually about as bad as possible. So, just using any old LCG wasn't good enough, you had to use one made by someone with a PhD in mathematics. Donald Knuth shook his fist at the world and shouted "Hah! I told you so!", published a book on how to do it Right that most people didn't read, and then went back into his Fortress of Solitude to write TeX. Because it was crated by IBM, RANDU's awfulness is now enshrined in history documents like this one, and then everyone went back to using whatever old crap RNG they were using anyway.

Also sometime in the 70's or 80's the arts of cryptography also started leaking from classified government works into the general world. People started thinking about the money they could make from scrambling satellite TV so that plebs with a HAM radio license couldn't get it, and started giving money to people who had PhD's in mathematics. It was quickly determined that neither LCG's nor LFSR's made good enough random-looking numbers to deter someone who knew how to use a slide rule, and since Edith had long ago retired to a small beach house in New Jersey, they needed to figure out how to make better random-looking numbers. But since doing this really well was slow and needed to do lots of things that nobody else cared about, that topic went on to its own adventures that will not be further mentioned.

Things more or less trundled along this way until the late 90's, when suddenly computers were everywhere and a new generation who had grown up too late to know how to use slide rules looked around and realized that their random-looking numbers really weren't very random-looking at all. Since they were doing a LOT of math with them by this point, this was actually sometimes a problem. The Mersenne Twister got invented, which was pretty slow and used a lot of memory and made kinda mediocre random numbers, but it was way better than a bad LCG so people put up with it. Most importantly, it had a cool name, and most people didn't want to read Knuth's book and figure out how to make a non-bad LCG, so everyone started using Mersenne Twister whenever possible.

This is where things stood until the early 2010's, when I finished my MS and started actually paying attention again. People suddenly realized it was possible to make random-looking numbers better than the Mersenne Twister using an algorithm called xorshift. Xorshift didn't need a whole 3 kilobytes of state just sitting around taking up space and causing comment at church, it was faster, and it still made pretty random-looking numbers. It did sometimes have problems with some of its numbers not looking random enough in a few select circumstances, but a couple people with math PhD's slowly and patiently spent years figuring out ways to work around these problems, leading to a whole confusing family of related things such as xoshiro, xoroshiro, xoroshiro+, xoroshiro*, and so on. Nobody else could really tell the difference between them, but everyone agreed they were better than Mersenne Twister, easier to implement, and the name was nearly as cool. Many papers were published and a good time was had by all.

At least until the late 2010's when some bright young spark figured out that it actually wasn't too hard, if you read Knuth's book and paid attention to what you were doing, to take the old LCG and hop it up on cocaine and moon juice. A few xor's were thrown in for style, and the result got called the Permuted Congruential Generator, or PCG. This quite miffed the people working on xorshift generators by being almost as small and fast, also using xor's and bit shifts (that's xorshift's turf, dammit, it's right in the name!), and producing random-looking numbers that satisfied even the people who had learned to use slide rules for fun in this latter age. Since nobody had figured out any downsides to PCG's yet, everyone shrugged and said "might as well just go with that then", and that is where, as of 2019, the art currently stands.