1# oorandom 2 3[](https://crates.io/crates/oorandom) 4[](https://docs.rs/oorandom) 5[](https://builds.sr.ht/~icefox/oorandom?) 6 7# What is this? 8 9`oorandom` is a minimalistic pseudorandom number generator in Rust. For 10those times when the `rand` crate is just too big and you want something 11a bit dumber. 12 13More specifically, it implements ONE prng, which is currently a permuted 14congruential generator (PCG). It may change if something better comes 15along, but that seems unlikely and will probably be a major version 16bump. It will give you `u32` or `u64`, and signed or floating-point 17equivalents. It is also `#[no_std]`. Anything else is gravy. 18 19Thanks to Lokathor for making 20[`randomize`](https://github.com/Lokathor/randomize), which inspired me 21to do my own equivalent. 22 23The name comes from my attempts to find a good way to pronounce 24`/dev/urandom`. 25 26Please direct questions, discussions and bugs to the [issue 27tracker](https://todo.sr.ht/~icefox/oorandom). 28 29# Why use `oorandom` instead of... 30 31 * `rand` -- `oorandom` is simpler and has zero choices you need to 32 make. It also compiles in 1/10th the time and has a stable API. 33 * `getrandom` -- They solve different problems; `getrandom` gives you 34 whatever secure randomness the OS decides to give you, not a 35 deterministic and seedable PRNG. It's generally a good idea to use 36 `getrandom` to seed this RNG though. 37 * `randomize` -- `randomize` used to be more complicated, but 38 `randomize` 3.x is quite similar to `oorandom` in functionality and 39 design. Go for it. 40 * `rand_pcg` and `rand_core` -- Yes you can take `rand` apart into its 41 pieces and use those individually, if you want to abandon having an 42 all-in-one solution, still deal with the lack of stability in 43 `rand_core` and actually figure out which pieces you need. It works 44 just fine. Seems more complicated than it needs to be though. 45 * `nanorand` -- `nanorand` uses the 46 [WyRand](https://github.com/wangyi-fudan/wyhash) PRNG algorithm, 47 which is supposedly faster than PCG and at least as good quality. I 48 haven't verified these claims, and I don't know of any *really* 49 thorough 3rd party investigation into them, though it apparently 50 passes [Dr. Lemire's tests](https://github.com/lemire/testingRNG). 51 So for now I personally consider WyRand to be in the "trust but 52 verify" level of quality. It's probably fine. Check back in 2027. 53 * `fastrand` -- Looks fine, uses the same algorithm as `oorandom`. 54 Made by the same people as the `smol` async runtime, which may be 55 good or bad for you. Does slightly more than `oorandom` does, which 56 may be good or bad for you. Use it if you like it. 57 58# This is not... 59 60This is not cryptographically secure, and if you use it for crypto you 61will get what you deserve. You are also in charge of choosing a useful 62seed; the `getrandom` crate might be useful for that. 63 64This is also not optimized to be stupidly fast, but is basically just 65as fast as `rustc` feels like making it. This means it is safe and robust 66and portable and involves absolutely zero clever tricks. 67 68# Usage 69 70```rust 71use oorandom; 72fn main() { 73 let some_seed = 4; 74 let mut rng = oorandom::Rand32::new(some_seed); 75 println!("Your random number is: {}", rng.rand_float()); 76} 77``` 78 79If you want a nondeterministic seed, I recommend using the `getrandom` crate to produce one. 80 81# License 82 83MIT 84 85# A brief history of random numbers 86 87The usefulness of random numbers has been known for a long, long time 88to people who also knew how to use slide rules. If you wanted to do 89some math without the bother of coming up with all that pesky input 90data from the real world, you might as well just use any ol' random numbers, 91as long as there weren't any patterns in them to heck up the patterns you were 92trying to look at. So in the first half 93of the 20th century you had little old ladies named Edith spinning 94roulette wheels or pulling bingo balls out of baskets and writing the 95results down, which got assembled into giant tomes and published so 96that engineering schools could buy them and have giant tomes sitting 97on their shelves. Anyone who wanted some meaningless numbers could 98pull the tome down, flip it open to a presumably-random page, and 99there were all the random numbers anyone could want. The problem was 100solved, and life was good. 101 102In late 1940's computers were invented, but they were far too big and 103expensive to be put on the task of *intentionally* generating 104nonsense, and things carried on as before. If you needed random 105numbers in a computer program, you just got a pretty young lady named 106Mary to transcribe part of the book to punch cards for you. 107 108Around the early 1960's computers got fast enough that Edith and Mary 109couldn't keep up with them, so they got downsized and replaced with 110more computers. To do this people came up with Linear Congruential 111Generators (LCG's), which could generate lots of numbers numbers that 112weren't really random, but sure looked random. LCG's worked well on 113computers that even a second-rate university could afford, and so the 114problem was solved, and life was good. 115 116At some unknown point in here, presumably sometime in the 60's or 70's, 117someone seems to have invented Linear Feedback Shift Registers (LFSR's) 118as well. These made random-looking numbers and were really easy to 119implement in hardware compared to the LCG, which needed to do 120complicated things like multiply numbers. The random-looking numbers 121made by LFSR's were good enough for hardware people, so they started 122using LFSR's whenever they needed to and never looked back. 123 124Anyway, by the late 60's people who knew how to use slide rules had 125realized that using numbers that only *looked* random could really heck 126up their math pretty bad, and one of the more common LCG implmentations, 127RANDU, was actually about as bad as possible. So, just using any old 128LCG wasn't good enough, you had to use one made by someone with a PhD in 129mathematics. Donald Knuth shook his fist at the world and shouted "Hah! 130I told you so!", published a book on how to do it Right that most people 131didn't read, and then went back into his Fortress of Solitude to write 132TeX. Because it was created by IBM, RANDU's awfulness is now enshrined 133forever in history documents like this one, and because the people 134writing OS's and programming languages at the time weren't actually 135doing much slide-rule stuff anymore and didn't actually *need* very good 136random-looking numbers, everyone went back to using whatever old crap 137RNG they were using anyway. The problem was solved, or at least not 138terribly problematic, and life was good. 139 140Also, sometime in the 70's or 80's the arts of cryptography started 141leaking from classified government works into the real world. People 142started thinking about how much money they could make from scrambling 143satellite TV so that plebs with HAM radio licenses couldn't watch it, 144and these people started giving more money to people who had PhD's in 145mathematics to figure out how to make this work. It was quickly 146determined that neither LCG's nor LFSR's made numbers that were 147random-looking enough to really get in the way of someone who knew how 148to use a slide rule, and since Edith had long ago retired to a small 149beach house in New Jersey, they needed to figure out how to get 150computers to make better random-looking numbers. But making numbers 151look random enough that someone couldn't undo the process and get free 152pay-per-view was slow and involved lots of details that nobody else 153really cared about, so that topic went off on its own adventures and 154will not be further mentioned. 155 156Things more or less trundled along this way until the late 90's, when 157suddenly computers were everywhere and there was a new generation of 158people who had grown up too late to know how to use slide rules, so they 159did all their math with computers. They were doing a LOT of math by 160now, and they looked around and realized that their random-looking 161numbers really weren't very random-looking at all, and this was actually 162something of a problem by now. So the Mersenne Twister got invented. 163It was pretty slow and used a lot of memory and made kinda mediocre 164random numbers, but it was way better than a bad LCG, and most 165importantly, it had a cool name. Most people didn't want to read Knuth's 166book and figure out how to make a non-bad LCG, so everyone started using 167the Mersenne Twister whenever possible. The problem was solved, and 168life was good. 169 170This is where things stood until the early 2010's, when I finished my MS 171and started paying attention again. People suddenly realized it was 172possible to make random-looking numbers better than the Mersenne Twister 173using an algorithm called xorshift. Xorshift was fast, it made good 174pretty random-looking numbers, and it didn't need a whole 3 kilobytes of 175state just sitting around taking up space and causing comment among the 176neighbors at church. It did sometimes have problems with some of its 177numbers not looking random enough in a few select circumstances, but 178people were gun-shy about their randomness by now so a few people with 179PhD's in mathematics slowly and patiently spent years figuring out ways 180to work around these problems, leading to a whole confusing family of 181related things such as xoshiro, xoroshiro, xoroshiro+, xoroshiro*, and 182so on. Nobody else could really tell the difference between them, but 183everyone agreed they were better than Mersenne Twister, easier to 184implement, and the name was nearly as cool. Many papers were published, 185the problem was solved, and life was good. 186 187However, at about the same time some bright young spark figured out that 188it actually wasn't too hard, if you read Knuth's book and thought real 189hard about what you were doing, to take the old LCG and hop it up on 190cocaine and moon juice. The result got called the Permuted Congruential 191Generator, or PCG. This quite miffed the people working on xorshift 192generators by being almost as small and fast, and producing 193random-looking numbers that satisfied even the people who had learned to 194use slide rules for fun in this latter age. It also used xor's and bit 195shifts, and that's xorshift's turf, dammit, it's right in the name! 196Since nobody had figured out any downsides to PCG's yet, everyone 197shrugged and said "might as well just go with that then", and that is 198where, as of 2019, the art currently stands. The problem is solved, and 199life is good. 200 201