Debian Codegraph @

debian/missing-sources/oorandom/README.md

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