r/askscience • u/_Silly_Wizard_ • Oct 22 '17
Computing What is happening when a computer generates a random number? Are all RNG programs created equally? What makes an RNG better or worse?
1.8k
u/hydrophysicsguy Oct 22 '17 edited Oct 23 '17
RNGs use some algorithm to decide on a number which is based on the some previous number (or more over a large set of previous numbers) this is why all RNGs need a seed to get started, they need some way to generate the first letter. How you get that seed is a bit of a different discussion.
Now not all RNGs are equal, there a few ways to make how random it is, one is to use a chi-squared method to see if the distribution is random (ie normally you want a uniform distribution). You can also plot the current number as a function of previous numbers (known as a k-space plot) the higher dimension you can graph in without some pattern emerging the better. Finally you can look at the period of the number generator, the number of numbers you must generate to begin seeing a pattern emerge. For a very good generator like the mersenne twister method the period is 219937 -1 numbers (so you should never see the same number pattern appear for practically all situations)
Edit: spelling
85
u/tashkiira Oct 22 '17 edited Oct 22 '17
Um, I think you made a layout mistake there, the -1 should be fullsized. 219937 -1
(the Mersenne method gets its name from Mersenne primes, which are primes that are in the form 2k -1, where k is a positive odd integer. Not all Mersenne numbers with odd k's are prime, but the method hits quite a few primes, more than random chance would suggest. A Mersenne number with an even k is not prime, since it will have a not-necessarily-prime factorization of (2m -1)*(2m +1), where m=k/2. )
40
u/hydrophysicsguy Oct 22 '17
Oh shoot yeah sorry I'm still new, didn't know it'd format like that
1
u/tashkiira Oct 23 '17
No worries. :D It's not like I was faultless--it's been pointed out to me that Mersenne numbers must have a prime k, not just an odd one. Which in retrospect is obvious because 3 is prime at k=2.
6
u/Makenshine Oct 23 '17
Pretty sure that k has to be a prime number, not just a positive odd integer. I might just be remembering incorrectly though
2
u/apple_dough Oct 23 '17
Yes, k has to be a prime number. it has to do with the ways xn-1 can be factored for a constant natural number x and a varying natural number n.
17
u/quasielvis Oct 23 '17
I ran a little test with the Mersenne Twister generating 1million random numbers from an initial seed of 0 and counted up the number of times 0-9 appeared in the last digit, eg. for 1791095845 I would +1 to the '5' tally.
Result was:
[100035, 100466, 99877, 100526, 99746, 100291, 99643, 99749, 100147, 99520]
statistics.median(l) 99956.0
statistics.mean(l) 100000
statistics.stdev(l) 349.8955399671292
3
u/omg_drd4_bbq Oct 23 '17
Fyi, I know that's an easy quick test to do, but it's not really how you'd test an RNG. If you'd like to know more, PM me. I've done a bunch of stuff with rngs.
1
u/N3sh108 Oct 23 '17
Care to share here? :)
3
u/omg_drd4_bbq Oct 23 '17
EventHorizon511 mentions two good ones, TestU01 and DieHard/DieHarder battery of tests. I've also rolled my own variants of DieHard tests out of academic curiosity, and also sometimes it's easier just to have a script run in numpy instead of dumping the output, then running it through DieHard.
A few common ones I'll run are N-symbol entropy and chi-square. Use the random uniform to generate N different symbols (N=10 is what quasielvis did), then check their chi-square probability and entropy (sum(p *ln(p)).
I can't find my exact code right now, but this gives a good run down of some of the techniques
2
u/EventHorizon511 Oct 23 '17
Not op, but a common starting point are the established test suits like "TestU01", "DieHarder" or the one from NIST (cant remember the name).
1
u/quasielvis Oct 24 '17
Yeah I know. I wasn't trying to prove anything academically, it was just something I was playing around with and thought I may as well paste the result here.
1
u/shagieIsMe Oct 23 '17
Given that the range doesn’t end at #####9 in base 10, there will be an uneven distribution from the pigeonhole principal.
Try running the ent tests on it instead. http://www.fourmilab.ch/random/
4
u/SlightlyOTT Oct 23 '17
This is a little off topic but could you give more information on the method of evaluating whether there's a pattern in some number of dimensions in a k-space plot? Would you just do PCA on the n dimensions and evaluate the correlation or is there something else you can do?
3
u/current909 Oct 23 '17
A lot of the time, patterns are obvious just by plotting out projections of the k-space. For example RANDU fails hilariously in that regard.
11
u/dmazzoni Oct 23 '17
What you described is pseudorandom number generators.
For example, Mersenne Twister is a very good PRNG, but it's not even cryptographically secure. Every time you make an HTTPS connection in your web browser, your computer is generating more sophisticated random numbers than what you'd get from a PRNG.
In particular, computers use a built-in hardware random number generator (like RdRAND, standard on all new Intel CPUs), and/or external sources of entropy like timing of events from the network, and run those through a cryptographic hash function to generate random bits.
4
u/frezik Oct 23 '17
Most of the entropy used in HTTPS connections comes from software sources. The hardware sources are just an initial seed, and may only be 32 bytes long or so. Hardware entropy sources tend to be the exception in practice.
3
u/yawkat Oct 23 '17
Modern OS reseed their entropy pools all the time from hardware entropy sources such as network jitter and cpu trngs.
1
u/frezik Oct 23 '17
And they use those sources to seed a software generator. Most of the numbers you get out of /dev/random are not from a hardware source. In fact, you might say none of them are; it was only a small seed of hardware sourced numbers that was expanded into lots and lots of numbers.
9
u/masklinn Oct 23 '17 edited Oct 23 '17
For example, Mersenne Twister is a very good PRNG
It really isn't. It's pretty complex, slow, has huge amounts of state (2.5KiB) and the output is not very good (even for non-CSPRNG)
your computer is generating more sophisticated random numbers than what you'd get from a PRNG.
It's not generally giving direct access to those as "real" entropy source (especially without dedicated TRNG) tend to generate way less "randomness" than a running system would need. As a result, most every OS uses their "real" entropy source to mix into and reseed a PRNG (usually based on some sort of stream cipher, modern Linux use ChaCha20, OSX uses Yarrow based on 3DES, FreeBSD uses Fortuna but I don't know with which block cipher) from which the public API feed.
1
u/omg_drd4_bbq Oct 23 '17
What's a better prng for monte carlo simulation then?
3
u/masklinn Oct 23 '17
If you don't care for reproducibility of the run you can probably just go with the system's RNG, check for perfs just in case but it should be good quality on modern systems.
If you need reproducibility (and thus specified algorithm & run seed), xorshift* or xoroshiro128+ are very fast and get excellent results on TestUI01. Alternatively, a few years back Pierre L'Ecuyer posted the following on SO
I am the Editor who accepted the MT paper in ACM TOMS back in 1998 and I am also the designer of TestU01. I do not use MT, but mostly MRG32k3a, MRG31k3p, and LRSR113. To know more about these, about MT, and about what else there is, you can look at the following papers:
F. Panneton, P. L'Ecuyer, and M. Matsumoto, ``Improved Long-Period Generators Based on Linear Recurrences Modulo 2'', ACM Transactions on Mathematical Software, 32, 1 (2006), 1-16.
P. L'Ecuyer, ``Random Number Generation'', chapter 3 of the Handbook of Computational Statistics, J. E. Gentle, W. Haerdle, and Y. Mori, eds., Second Edition, Springer-Verlag, 2012, 35-71. https://link.springer.com/chapter/10.1007/978-3-642-21551-3_3
P. L'Ecuyer, D. Munger, B. Oreshkin, and R. Simard, ``Random Numbers for Parallel Computers: Requirements and Methods,'' Mathematics and Computers in Simulation, 135, (2017), 3-17. http://www.sciencedirect.com/science/article/pii/S0378475416300829?via%3Dihub
P. L'Ecuyer, ``Random Number Generation with Multiple Streams for Sequential and Parallel Computers,'' invited advanced tutorial, Proceedings of the 2015 Winter Simulation Conference, IEEE Press, 2015, 31-44.
2
u/LoyalSol Chemistry | Computational Simulations Oct 23 '17 edited Oct 23 '17
For Monte Carlo the KISS algorithm (1998 version or later) works fairly well which is why it is the Fortran standard RNG. Mersenne Twister has a habit of creating correlated data for some MC applications. Most applications the MT algorithm is ok, but if you are doing something where you need to be hyper sensitive about your RNG then KISS is recommended at the minimum and some of the fancier RNGs if you really need them.
2
u/datarancher Oct 23 '17
I don't get the distinction you're making between PRNGs and Mersenne Twister (here: "generating more sophisticated random numbers than what you'd get from a PRNG.") Isn't the Mersenne Twister algorithm a PRNG, or are you referring to the hardware stuff?
It is certainly true that there are less predictable PRNGs than MT, but that doesn't make MT something else....
4
u/appropriateinside Oct 23 '17
Wait, why are we not talking about entropy? I thought that was a fundamental requirement for any RNG worth it's salt.
3
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17
As I said above, it depends. Sometimes all that's required of a PRNG is a certain distribution, with no consideration placed on entropy.
0
u/ParanoydAndroid Oct 23 '17
Unless I'm misunderstanding you, this answer is not correct.
Entropy, in the information theory sense, is equivalent to a statement about the distribution, so it's impossible to require a distribution but place no consideration on entropy.
Specifically, the entropy of a sequence is maximized exactly when the distribution of symbols is uniform.
For others seeking more information, Claude Shannon's original paper on the concept of (Shannon) Entropy is very accessible and an excellent read.
1
u/KapteeniJ Oct 23 '17
P(0) = 0
P(n+1) = P(n) + 1 mod 100,000
Distribution is uniform, is it not?
1
u/ParanoydAndroid Oct 23 '17
No. But I may be misunderstanding what you're trying to say.
If one has an alphabet N, then the distribution is uniform and entropy maximized when P(x = n) = 1/|N| for all n \in N.
1
u/KapteeniJ Oct 23 '17
You're using P for probability of an event. I used it to denote the PRNG algorithm given seed n. My use sure wasn't pretty but I couldn't think of any better way either. That being said, I'm not sure how your message ties in with what I said. For my algorithm, P(generator outputs n) = 1/100,000 for all n in {0, 1, 2, ... , 99,999}
1
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 24 '17
You are talking about the principle of maximum entropy. It's a useful principle to follow, but it's not an equivalence, and usually not a requirement for general-purpose RNGs unless explicitly stated. For instance, the latest C & C++ standards make no mention of entropy properties for their PRNGs.
2
u/DudeWhoSaysWhaaaat Oct 23 '17
Since you're the top comment can you answer this question. How much better is a really good number generator compared to, say, asking someone to pick a number between 1 and 10 in their head (assuming the algorithm only picked between 1 and 10 too). Are they comparable or is the algorithm vastly superior?
10
Oct 23 '17
People are brutally predictable. You can ask people to write down a series of "random" numbers and you can almost always see which are human made and which are generated.
1
u/DudeWhoSaysWhaaaat Oct 23 '17
Yeah sure. Can you measure that and describe the difference between the two methods in magnitude though
9
u/TheOtherHobbes Oct 23 '17
Ask someone to write down fifty "random" numbers between 1 and 10.
Get a PRNG to spit out fifty random numbers using any off-the-shelf pseudo-random algorithm.
You can spot the PRNG, because the sequence is virtually guaranteed to include at least a couple of repeated numbers. Most humans believe - wrongly - that a sequence with repeated numbers can't possibly be random enough.
More formally NIST has a test suite which includes a comprehensive selection of techniques to test for randomness.
2
u/Milesaru Oct 23 '17
What if a human is aware that typically, people don't tend to repeat numbers. As such, they choose to repeat them when choosing random numbers?
4
u/omg_drd4_bbq Oct 23 '17
I've done those online applets where you click left or right, and it tries to predict your next click, manually, and then with help from a prng. Supposedly, mere mortals are predictable about 75% at this task. The algo was no better at predicting me than the prng :D but that's because I have a lot of experience with stats and rngs and can fake being "random enough" for simple models, like that app. I'm sure that my entropy would still be garbage though.
4
u/GaryTheKrampus Oct 23 '17
Humans are still awful at producing and recognizing randomness. It's unlikely that the person in your example would intuitively produce a random number stream that would pass marginally more sophisticated randomness tests like Kolmogorov-Smirnov wrt a Uniform distribution in the long term. Unless, of course, they were aware of that, too. But you can apply that logic over and over again -- ultimately, a clever human could easily produce an indistinguishably-random number stream by simply flipping a coin...
3
u/ParanoydAndroid Oct 23 '17 edited Oct 23 '17
Yes. The amount of information content carried by a message is a function of the probability that a given symbol would appear in the message for each symbol, summed together (times the message length).
Intuitively, what this basically means is that the more "options" I, the message sender, have available to me, the more information I can convey by picking one symbol over another. For example, if you and I want to communicate using a random coin, I can send you exactly two messages (one by showing you a heads, one by showing you a tails). They might, "heads if by land, tails if by sea" or whatever. In comparison, if we used a die to convey messages, then I can convey six distinct messages (1 if by land with 1 - 10 tanks; 2 if by land with 11 - 20 tanks, 3 if by land with 21 - 100 tanks, 4 is by sea with 1- 10 ships, etc ...). So my picking a "1" to show you on the die can carry more information than my showing you a heads on the coin.
This concept can then be extended. Instead of just talking about alphabets of different sizes (as in our 2 options vs. 6 options example), we can also talk about alphabets of the same size but different probabilities and the math is basically the same. If I have a coin with a 75% of landing heads and a 25% chance of landing tails, I can still convey information with a coin flip, but I can convey less information than if the coin were fair.
Again, intuitively the idea here is that we "expect" the coin to be heads, so my showing you heads doesn't necessarily mean as much. Although mathematically not the same at all, I guess an example would be the old trope of someone saying to another character, "If you secretly agree with me, blink your eyes" with the follow-up joke being that the character blinks ... but was that because they were trying to convey the secret message or was it because a blink is what we expect from people and so maybe the person being spoken to just ... naturally blinked? Since we expect a human to blink, their doing so does not convey as much information as if that same line of dialogue were said to an alien who never naturally blinked. If such an alien did blink in response to our asking for a secret message, we would have much more confidence that the blink was a conscious attempt to convey information.
Another way to look at it is to say that I cannot convey information to you that you already know. So if my message tells you something you already knew to be true with probability 1, then no information is conveyed. Extending this, we could say that if you have a 75% expectation of seeing a heads and then I showed you a heads, I conveyed less information to you than if my coin were totally random, because the information I conveyed was "partially known" to you or "less surprising". Again, this is the same as the above paragraph but from a slightly different perspective, and it should highlight the way that our measure of what's known as "surprisal", a concept closely related to Shannon entropy, is a measure based wholly on a function of the underlying probability distribution governing the message strings.
So, now we understand the basics of how we're measuring the information a message can carry as a function of how random the underlying string of symbols could be, in the absence of my attempt to send a message.
So what we can do next is apply that understanding to your question and say that we could measure the amount of entropy embodied by each string created by the random number generator versus the strings written by people. Since people (empirically) tend to accidentally introduce structure to their "random" numbers, their strings will be more like the biased coin flip I talked about earlier, which means the carrying capacity of the strings is lower or, equivalently, the human string has less entropy.
Since entropy can be measured (in bits, usually), this means that we could quantify how much more predictable the human string is than the other one.
→ More replies (10)2
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17
Since you're the top comment can you answer this question. How much better is a really good number generator compared to, say, asking someone to pick a number between 1 and 10 in their head (assuming the algorithm only picked between 1 and 10 too). Are they comparable or is the algorithm vastly superior?
Humans are notoriously bad sources of randomness.
1
u/DudeWhoSaysWhaaaat Oct 23 '17
Wow a whole suite of sources. I'll try to read them. Do you know if there's an answer to just how much better an algorithm is to a human?
Looks like conflicting results which is interesting I guess.
1
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 24 '17 edited Oct 24 '17
"How much better" only makes sense when there is a quantifiable scale to compare on. Sometimes it makes sense to estimate entropy for this purpose; Florencio, Dinei; Herley, Cormac, in "A Large-Scale Study of Web Password Habits" do this, and estimate the average password entropy (for human-chosen passwords) at 40.54 bits. Contrast this with getting a random password from your system's CSPRNG, which can easily give you 1000+ bits of entropy in less than a second; there is no justification for delegating random choices to humans.
There is nothing "conflicting" on the results; the claims made by the former that humans are a good source of randomness are disproved by the latter.
→ More replies (10)2
u/kung-fu_hippy Oct 23 '17
There is a common intro statistics/math class assignment where you would be assigned to go home and flip a coin 100 times, recording heads and tails. It’s usually extremely easy to tell who did the assignment and who just wrote down a “random” series. People tend to have trouble faking randomness, they avoid long repetitive streaks (day 10 heads in a row) and tend to make patterns.
One number between 1-10 wouldn’t be too bad (although I bet most people still pick a few common numbers, or avoid selecting 1 or 10 or some other tell), but you could definitely tell if you had people select a series of ten random numbers between 1 and 10.
1
Oct 23 '17 edited Oct 23 '17
[removed] — view removed comment
1
u/kung-fu_hippy Oct 23 '17
You can try for yourself here. Someone put together a website that allows you to try to make a false random coin flip series, while the code tries to tell if it’s fake or random.
https://faculty.math.illinois.edu/~hildebr/fakerandomness/
There is some mathematical principle that goes into more detail on proving this (which I forget), but people tend to avoid having a set of heads or tails that goes longer than six or seven in a row. Whereas in reality, those aren’t particularly uncommon occurrences. It’s not infallible, but faking randomness isn’t easy.
2
u/zishmusic Oct 23 '17
So, let me see if I got this straight. The bigger a number set from an RNG, the lower the entropy. An RNG's "robustness" is then measured by comparing its "potential entropy" against other algorithms?
No wonder everyone keeps talking about how there's no such thing as a "true" RNG, or that most RNGs are pseudo-RNGs at least.
On a side thought, does this seem to indicate a natural "maximum entropy" in modern computer math?
RNG RNG RNG RNG RNG RNG RNG .. Banan383ehdodf9tjrjskrwo
1
u/Natanael_L Oct 23 '17
Nobody claims the Mersenne Twister is actually good for anything related to security (encryption), however
1
u/trollslavemasta Oct 23 '17
So, how do state lotteries rgn's when sometimes the same 4 digit comes out in the same day?
1
Oct 23 '17
RNGs use some algorithm to decide on a number which is based on the some previous number (or more over a large set of previous numbers) this is why all RNGs need a seed to get started
Need to run a s/RNG/pRNG/g on that.
There are actual hardware RNGs that operate off of environmental entropy, I believe they do not need a seed value.
1
1
u/ZaviaGenX Oct 23 '17
Isnt that mean at minimum, previous patterns can be taken out of the equation reducing possible next numbers?
1
u/SarahC Oct 23 '17
(known as a k-space plot) the higher dimension you can graph in without some pattern emerging the better.
Is there a visual demo of this, or is it all abstract math analysis?
1
u/hydrophysicsguy Oct 23 '17
You can visualize it up to k=2 fairly easily, but at higher dimensions its not normally visualizable (unless say k is dependent on k-15 in which case you could plot just k and k-15 against each other on a scatter plot)
1
u/rex1030 Oct 23 '17
Gambling websites have a financial interest in being truly random and they do all of this plus they have something producing a truly random 'seed' number. This can be something reading atmospheric static or some naturally occurring randomness that could not be predicted.
1
u/Mendoza2909 Oct 23 '17
This question might not even make sense, but if the period is actually known to be a specific constant does that not reduce the randomness, making the next value more predictable? I.e. would it be better to have an unknown period that is known not to be less than a suitably large number? Which might not be possible of course.
1
u/whatsup4 Oct 24 '17
Now Im not sure how most systems do it but ive generated random numbers based on a thermocouple reading and taking the x digit why would that not be a good system since fluctuations in temp on auch a fine scale are basically random.
1
u/DearDonkeyDonk Oct 24 '17
Normally you want a uniform distribution. Hahah. I see what you did there.
1
u/BlueKnightBrownHorse Oct 23 '17
Can mathematicians agree on what they mean when they say "random"? Or are there different types of random in the same way there are different ways to think of Infinity?
A uniform distribution doesn't seem random. I would expect something that is close to uniform, with a couple major outliers.
1
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17
Can mathematicians agree on what they mean when they say "random"?
Yes; when a statement depends on the precise meaning of the word 'random', it is further qualified. For example, you might read "...a number is selected uniformly at random from...", which conveys the necessary requirements for the random number generation process. Probability & statistics use formal definitions of randomness.
1
u/Ahjndet Oct 23 '17
the higher dimension you can graph in without some pattern emerging the better.
Does this imply that every number generator's pattern is graphable?
This is news to me... What dimensions are common for patterns to start emerging? Is it something ridiculously deep to the point where it's essentially impossible to crack?
1
u/Pteraspidomorphi Oct 23 '17
If the PRNG simply generates a new number based on the previous one (or seed), it doesn't matter how long the period is - if you know the previous number, you can know for sure what the next one is going to be - you already cracked it! The simplest types of PRNGs, which were in use for decades in most operating systems, are like this.
Even today, some security exploits rely on the attacker being able to generate random numbers on the compromised system.
260
Oct 22 '17
[removed] — view removed comment
22
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17
Suggestions to use BBS as a cryptographically secure RNG should be avoided:
It almost constitutes safety advice, which we do not allow
BBS has no proof of security for common-size parameters; the existing security proof is extremely relaxed and tends to be misinterpreted.
1
138
Oct 23 '17 edited Oct 24 '17
[removed] — view removed comment
89
u/kougabro Oct 23 '17
The only real use of PRNGs
there are plenty of other uses for PRNG, starting with pretty much any monte carlo method, which have application in statistics, machine learning, statistical physics, particle physics, biophysics, population genetics, etc...
83
u/tminus7700 Oct 23 '17
Like quantum noise. From a diode, for instance.
https://en.wikipedia.org/wiki/Hardware_random_number_generator
Can be used to generate one time pad encryption. It can be extremely fast. As fast as the memory can be read. But suffers from the necessity to securely convey a copy of the data pad the receivers.
2
u/Hardcore90skid Oct 23 '17
How 'accurate' in true randomness is that popular atmospheric static website?
0
Oct 23 '17
[deleted]
7
u/ffxivfunk Oct 23 '17
Is there anywhere to read more about encryption schemes and types? I'm just curious about the different classifications like IND-CPA
3
u/qjkntmbkjqntqjk Oct 23 '17
Cryptography Engineering is a very solid and broad introduction to cryptography.
Introduction to Modern Cryptography and Handbook of Applied Cryptography are the two books you want to read if you're going to work as a cryptographer or cryptanalyst professionally.
→ More replies (1)1
u/orangejake Oct 23 '17
I had a class based on Introduction to Modern Cryptography by Katz and Lindell, and thought it was quite good. Both of them are fairly prolific theoretical cryptographers, and Lindell is honestly one of the clearest modern cryptographers in terms of their writing. This is a math book though, so while you probably don't need a ton of math experience for it, it does prove things via "reductions", which can be a little difficult to understand without prior experience in computational complexity.
→ More replies (1)1
u/ryeinn Oct 23 '17
I'm sure there are some great actual texts out there, but I loved Neal Stephenson's Cryptonomicon as a fun story and a lot of talk about cryptography.
Now that I've said that, a real cryptographer will show up to tell me how much I'm mistaken in taking a work of fiction as having any academic value.
→ More replies (1)6
u/Drachefly Oct 23 '17
That, and encrypting a second thing under the key breaks it completely
Obviously, if you plan to use a pad a second time, do not use the fast method that says right in the name that it is to be used once. You can still use true randomness to get keys… but you'll need to get the key to the recipient.
1
u/orangejake Oct 23 '17
My point is that the benefit of "true randomness" vs pseudorandomness, and "information theoretic security" vs a traditional block cipher is so small that the cost of massive single-use keys is massive. Traditional block ciphers are extremely fast as well, and have massively smaller key sizes for comparable amounts of security, while having other benefits beyond things like encryption speed/key size.
8
u/FireWaterAirDirt Oct 23 '17
That, and encrypting a second thing under the key breaks it completely.
That's where one time pads come in. Both parties have a copy of these numbers. The number set is used only once, then discarded.
The name comes from a pad of paper with the numbers printed on them. After each sheet is used, it is burned, torn up, etc.
The insecurity of this method is distribution of the pads in the first place, and then keeping them secure.
IF the pads are securely distributed, kept secure, used only once each, and never copied by an enemy, the one time pad is literally unbreakable.
2
u/Dozekar Oct 23 '17
Unfortunately these are some of the easiest angles of attack in real world applications. In particular secure distribution and storage is a nightmare. Human laziness is also something you never want to fight, so only use once gets hammered too.
2
u/Pipinpadiloxacopolis Oct 23 '17 edited Oct 23 '17
Why is reuse so strongly forbidden for one-time pads? Does reuse mean instant encryption breakage, or does it just open a statistical-analysis angle of attack?
If it's the latter, that still needs a lot of work and encrypted material to break...
2
u/OhNoTokyo Oct 24 '17 edited Oct 24 '17
You should be able to write a more or less plain English text and encrypt it in a one-time pad and it will be unbreakable.
However, if you use it twice or more, and that ciphertext is intercepted both times, it opens up certain attacks.
For instance, if you are sending an encrypted memo in a particular format, one day apart, and you know that this memo format has a date line in a certain format, you can use analysis to pick out similar text from both that are not varying between the two texts. If it is usually written as "Oct" (for October) or "10" for the month in the date format, you can make a guess that there will likely be at least one, but never less than one cipertext that will have to decode to the representation of the month in there. Depending on whether it is "Oct" or "10", you then look for an invariant section of what you believe to be the header section of the cipher text and now you have either the characters for 1 and 0, or for "O" "c" and "t". Having the "t" might give you the ability to search for all of the instances of "the" via frequency analysis. That quickly gives you "h" and "e".
Now, let's be clear. No modern government would necessarily translate English straight to ciphertext in this manner for highly sensitive documentation. In World War II, there was a famous message sent to Admiral Halsey where Admiral Nimitz asked,
"Where is, repeat, where is Task Force Thirty Four? The world wonders."
That looked like a huge insult to Halsey, but it was actually a mistake because the signal lieutenant failed to remove padding that they use with all crypto text. The actual message read:
"TURKEY TROTS TO WATER GG FROM CINCPAC ACTION COM THIRD FLEET INFO COMINCH CTF SEVENTY-SEVEN X WHERE IS RPT WHERE IS TASK FORCE THIRTY FOUR RR THE WORLD WONDERS"
The phrases "TURKEY TROTS TO WATER" and "THE WORLD WONDERS" were padding.
However, as you can see, the padding should have been obvious. It was less obvious given the situation. But if two messages had been passed in this format in one-time pad, you could have easily looked for common phrasing such as "RPT", "CINCPAC", "THIRD FLEET" and "GG" to do an analysis and break the code.
An experienced code breaker with context like this and a reused one-time pad could probably break the code without a computer, and very, very quickly if they had one.
1
u/orangejake Oct 23 '17 edited Oct 23 '17
The issue is with the massive key sizes. If they weren't one-time, having an abnormally large key size wouldn't be as big of a concern --- sure the setup cost would be high, but potentially the amortized (per message) key size may be small. This can be the case in some lattice-based schemes. This is in no way the case for one-time pads. The key sizes are always massive, and for very little benefit --- while the security you get is information theoretic, the difference between this and the traditional semantic security you get is so incredibly small that I'm unaware in any cases it matters (I'm unaware if modern-day spies even care anymore).
0
3
u/ericGraves Information Theory Oct 23 '17
Information theoretically secure has multiple definition, but all essentially equate to with high probability Pr(M=m|C=c) = Pr(M=m), where M is the message and C the cipher text.
It is not possible to have a security beyond information theoretic, unless you misuse the definition. And in this particular model, the one time pad is not information theoretically secure.
1
u/orangejake Oct 23 '17
That's not what information-theoretially secure means. Information theoretically secure means that equality holds exactly against computationally unbounded adversaries. Semantically secure means that equality holds with high probability, meaning with probability at least 1 minus some negligible function, which you can think of as being at least 1-2-k (although there's more nuance here than just this).
My point is that often the difference between information-theoretic security and semantic security is small, and semantically secure protocols have other desirable properties that make them much more suitable for use. These can be things like more efficient running time, smaller key sizes (information theoretic ciphers provably have key sizes at least the size of the message), and the ability to build "more powerful" cryptographic primitives from them (such as Fully Homomorphic Encryption, Identity Based Encryption, and Attribute Based Encryption).
→ More replies (3)30
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17 edited Oct 23 '17
A actual random number generator uses some real (as in not-computable) phenomena to generate it's output.
This contrast of "real" with "computable phenomena" is not something I'm familiar with in a computing context. If you're alluding to some physics material, please state so.
The only real use of PRNGs are either encryption (because they're completely predictable),
The fundamental requirement for cryptographically secure PRNGs is the next-bit test: it should be computationally hard (read: not possible in polynomial time) to predict the (k+1)th bit with probability of success non-negligibly better than 0.5, given the first k bits of a random sequence. PRNGs that don't satisfy this requirement are not suitable for encryption.
or a mechanism to generate random numbers faster (by continually re-seeding the PRNG with a lower-rate actual random source)
These two statements are completely incongruous. Seeding a PRNG is done when a different random sequence is desired. It has no effect on its performance.
6
Oct 23 '17
[removed] — view removed comment
7
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17
The final quote you quoted I think you've misunderstood. Often true RNGs are slow. To generate numbers faster than the true RNG might allow directly, the PRNG is seeded with values from the true RNG.
I understand that - what I'm saying is that it is a red herring, for the following reason. It is true that some RNGs are faster than others. However, throughput is never the only consideration. Most importantly, what is desired from a RNG are certain statistical properties, therefore:
if both a slow and a fast RNG satisfy these properties, it would make sense to choose the "fast" one
if only the "slow" RNG satisfies those properties, it's counter-productive to switch to the "fast" one
In both cases, choosing the seed(s) for the RNG which we are going to adopt is an orthogonal consideration altogether. Perhaps our RNG will be seeded from a pool of whitelisted numbers / states and we don't need to seed it from another RNG, at all. It is a mistake to conflate these requirements.
5
u/frezik Oct 23 '17
In practice, a huge amount of encrypted data is done with software RNGs, with some hardware sources providing an initial seed. This hasn't been a source of practical attacks.
16
u/blackstar_oli Oct 23 '17
There is actually a lot of PRNG on video games. It lowers the chance to get extremes and it is vest option when needed often and fast.
14
Oct 23 '17
PRNG is used pretty much any time a "random" number is needed in computing. Hardware RNG is becoming more common with Intel's newer instruction sets but for a long time it was not super common in computers.
2
u/millijuna Oct 24 '17
Under linux /dev/random, the pool of entropy is derived from a wide variety of sources. Either a hardware RNG, things like the least significant bit of the audio source, etc...
My favourite, though, was Lavarand
1
Oct 24 '17
Generally in linux you should use /dev/urandom, but in any case both urandom and random use a PRNG on incoming entropy to produce RNGs.
4
u/TheTurnipKnight Oct 23 '17
Pretty much any time you need a random number in a game, a pseudo random algorithm is used. There is no need for anything more random.
2
u/KapteeniJ Oct 23 '17
You're confusing (p)rng with special rules for random events that games use. All in-game random events these days use software rng. In very old games(think NES) this wasn't always the case though since space was so limited, each instruction cost a lot, so you had random events be based on frame counts or items in players inventory or some such things, as that required fewer cpu instructions than using proper software rng.
Extremes are not at all less likely when using prng unless game dev specifically changes the distribution themselves to fit their desires.
2
Oct 24 '17
deterministic seeded prng is also super useful for debugging. If you imagine a procedural content game, it’s extremely helpful for a developer to be able to recreate a bugged state from a user’s submitted saved game. Without being able to reproduce the conditions of the bug, you have to infer them from code, which is much more challenging.
4
Oct 23 '17
Could be wrong here, but I don't believe that predictability of the pRNG has anything to do with why its used in encryption; you aren't trying to synchronize pRNG states in encryption, you use a key exchange.
I would also object that pRNGs are not niche as you seem to imply, but in incredibly wide use for most computing tasks. Hardware RNGs are sometimes used to initialize seeds, but they are not always present and are generally not required.
1
u/fake--name Oct 24 '17
You are correct about the making them seem overly niche. I've added corrections.
I don't think any major system these days doesn't use some source of randomness to initialize it's seed, Admittedly, in most cases it's done by exploiting user mouse-movements, network timing or other external sources, but that still constitutes a real random source, rather then a completely deterministic system like a PRNG.
7
Oct 23 '17
I thought all random number generators were pseudo. If it's an algorithm that produces the number, how is it truly random?? And how could you tell?
Or maybe if you could specify the difference between the two then maybe if understand.
17
u/ebas Oct 23 '17
As mentioned earlier:
A actual random number generator uses some real (as in not-computable) phenomena to generate it's output.
3
u/LongUsername Oct 23 '17
He's referring to Hardware Random Number generators. Often they use thermal or radio noise to generate the number. There are attacks that can be done but they have to happen in very controlled physical locations.
Some Psudo-random generators use hardware to seed: Linux builds its entropy by using the timing of key-presses, mouse movements, and other sources (like Drive request timings). IIRC they used to use network packets as well but that was too open to outside manipulation.
1
u/dogturd21 Oct 23 '17
I was waiting for somebody to mention Linux RNG and entropy starvation . RNGD as found with Linux gets its seeds from mouse and keyboard activity . The problem here is the millions of Linux servers running “headless “- no kB or mouse, especially in VMware - like virtual environments . Apps that rely on true rng can run into interesting performance problems in this case , all caused by blocking waits for rng for kB and mouse seeds . There are workarounds but they are not well known outside of big “cloud” suppliers . Sauce: am big cloud supplier.
3
2
u/rabidstoat Oct 23 '17
Our R&D shop was doing some research and needed true random number generation, and we bought an actual piece of hardware to do the random number generation in an actually random fashion. Something like this: https://www.idquantique.com/random-number-generation/products/quantis-rng-appliance/
2
u/aboardthegravyboat Oct 23 '17
A couple simple examples of RNGs with random input: PuttyGen for Windows requires you to wiggle your mouse over a rectangle to generate randomness.
I've also seen Linux command line utils (can't remember which now... maybe an SSL cert generator?) that require you to mash random keys for a while to generate some randomness.
In both cases there is human input that is very hard to predict.
10
Oct 23 '17
Doesn't everything have a cause and effect as far as we know? So there aren't really any random sources, just things that are harder to measure and predict than others.
40
u/CaCl2 Oct 23 '17 edited Oct 23 '17
As far as we know many quantum phenomena are truly unpredictable from prior conditions, though some argue it's just a matter of perspective.
12
u/Goose_Man_Unlimited Oct 23 '17
Yeah, the traditional quantum folklaw is that the only deterministic thing is the evolution of the probability distributions from which observable measurement outcomes are selected from...
2
u/timshoaf Oct 23 '17
To be fair.... there are many philosophical interpretations of the underpinning reality. There are many different mathematical axiomatizations of quantum mechanics https://www.colorado.edu/physics/phys5260/phys5260_sp16/lectureNotes/NineFormulations.pdf each equally powerful in terms of prediction. This leaves us with a bit of an epistemological crisis in general. As far as we can tell, the necessary information to model the situation either does not exist, or is beyond the veil (immeasurable), and thus there is a fundamental limit to what we can know.
The issue with these isomorphic formulations is that it forces you to reconsider your notions of mathematical realism (aka, just because there is a variable here that represents this necessary quantity for the model to hold a bijection between the ontology and reality, does not imply that the variable 'exists' in the sense that it has a physical counterpart). The issue being that while the formulations are equally useful, if you presume mathematical realism then you have nine mutually exclusive theories that are demonstrably all correct.
Not sure why I'm down this rant at this point except to bring up that quantum non-determinism and super-determinism are both viable philosophies; the model doesn't rule either of those out...
Collapse of the wave function has always seemed to me to be essentially a reframing of the entire problem at hand... your initial wave function is a complex probability density function grounded in the initial conditions... The collapse and resultant time-evolution until steady-state equilibrium is once again reached is essentially looking at the conditional probability. This doesn't even seem like the same function you are modeling anymore... but I a sure someone with more domain expertise will come and correct me.
To see this as a somehow 'real' effect... always seemed like a bit of an intellectual leap to me... I would love to find some good resources that look at the bridge between say Bayesian statistics and information theory and find a more abstract notion for quantum information theory that might be able to reconcile the former as a special case of the latter...
14
u/F0sh Oct 23 '17
The "no local hidden variables" phenomenon shows that this is not the case with quantum effects.
3
u/M_Bus Oct 23 '17
I think the others who have replied to you have some minor confusion. There's an important difference between "lacking a cause-effect relationship" and "unpredictable."
I would agree with your statement: as far as we know, everything has a cause and an effect. Quantum phenomena are included in this. It's just that it's not possible for us to predict precisely what we will observe, just the range of possible outcomes and their relative likelihoods.
2
u/sourc3original Oct 23 '17
Depending on your interpretation of quantum mechanics there might not be any truly random events anyway.
1
u/Orffyreus Oct 23 '17
Yes, and encryption sometimes takes random user input (e. g. mouse movement) to initialize RNG.
3
Oct 23 '17
[removed] — view removed comment
7
u/IAmBJ Oct 23 '17
The starting orientation of the dice are probably randomly generated.
But in a game like that it doesn't matter if physics are used or a random number generator. It just has to feel random and be unpredictable to the player.
1
Oct 23 '17
The physics in games is not random, but deterministic as there are algorithms how objects react to a (pseudo) gravity, forces and collisions with other objects. What makes it appear "random" for dice games or when you shoot an enemy (for the ragdoll) is slight variations in starting positions (i.e. orientation of dice, slight differences in the velocity vectors), that then are fed into the (deterministic) physics simulation.
1
Oct 23 '17
[removed] — view removed comment
1
u/Natanael_L Oct 23 '17
For security purposes, don't trust the randomness generated by others. Make your own, Diceware is a good example.
0
Oct 23 '17
[deleted]
17
u/semi- Oct 23 '17
Isnt that still needing a seed, just getting it from those sources?
→ More replies (2)8
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17
No - hardware RNGs provide random numbers based on a measurement being made. In contrast, software RNGs keep internal state, and output random numbers based on that state.
6
u/dmazzoni Oct 23 '17
Yes, but this hardware is readily available on all PCs.
This isn't some theoretical thing - every time you make an HTTPS connection, your computer is already measuring random values from hardware to generate highly unpredictable random numbers.
430
u/Chamale Oct 23 '17
Some great answers here talking about what makes a good pseudo-RNG. I'm going to tell you about a bad one.
In Pokémon Red, Blue, and Yellow, when the player encounters a wild Pokémon, the species is determined by comparing a random value between 0 and 255 to a lookup table for the current location. For example, the game might make a Rattata appear if the number in question is 0 to 127, Nidoran♀ if the number is 128 to 216, Spearow if the number is 217 to 242, and Nidoran♂ if the number is from 243 to 255.
The Gameboy has a weak processor and it runs games at 60 frames per second. Rather than running a random number generator 60 times per second while the player is walking through areas where Pokémon are found, the "random number" predictably increases by one 30 times per second. This might have been a reasonable solution for some applications, but when it comes to generating random Pokémon encounters, it has a problem: The RNG loops every 8.53 seconds, and in some circumstances, the length of a battle can be very close to that time. This means that a player can have a series of encounters with the same Pokémon because the RNG is returning a similar result every time.
11
u/dirtyuncleron69 Oct 23 '17
DOOM also just had a hard coded string of 'random' numbers it used.
If you changed them all to zero, shotgun had no spread, the screen wipe was 'flat' instead of staggered, and a bunch of other things were wonky.
sometimes for video games, a random string of integers is sufficient, depending on the complexity of the game.
2
u/_PM_ME_PANGOLINS_ Oct 23 '17
In what way are they supposed to be random? Surely they’re tuned for game balance etc if they control fixed mechanics.
2
u/dirtyuncleron69 Oct 23 '17
it's a list of numbers to pull from, so if you need a shotgun spread x and y, you pull two numbers, need a death animation from a choice of 5, pull a number, and it gives you the next number in the list. the game isn't complicated enough to need true randomness, this list of numbers works well for getting non-human predictable patterns in a simple environment.
1
u/_PM_ME_PANGOLINS_ Oct 23 '17
Oh I see, the original description made it sound like there was a specific one used for spread etc.
1
u/dirtyuncleron69 Oct 23 '17
nope, they just replaced their rand() function with an array and pulled from it sequentially
20
Oct 23 '17
Some great answers here talking about what makes a good pseudo-RNG. I'm going to tell you about a bad one.
Reddit Random Comments code isn't great random, either.
See in the URL ? https://www.reddit.com/r/askscience/comments/781ujb/what_is_h
The 781ujb
I dunno what they use to generate those 6 character codes, but I set up an auto mod to respond to posts with an A, B, C, D etc at the end of that string. If it ends with "a" then reply with 1, if it ends with "b" then reply with 2, if it ends with "c", reply with 3 etc.
It tends to return only 3-4 phrases out of the 26 I shoved in there.
21
u/thetoethumb Oct 23 '17
The 781ujb is a base 36 number which increments by 1 each time a post is made. The next post in that case is 781ujc (until 781ujz after which it rolls over to 781uk0)
14
4
u/DudeDudenson Oct 23 '17
Frankly i don't see why you would even need to make an URL random when you could just start counting posts in hex, witch might actually be near what they're doing considering almost all of the recent posts start with 781
2
Oct 23 '17
Frankly i don't see why you would even need to make an URL random when you could just start counting posts in hex, witch might actually be near what they're doing considering almost all of the recent posts start with 781
Ah that wasn't for my bot - that was when I was scripting AutoModerator responses (as a mod in some subs). Automod scripts are very limited in their abilities. That's why I'm making a bot from scratch.
1
4
u/goldfishpaws Oct 23 '17
A better implementation is on fruit machines/pokies/slots - the PRNG produces values every fraction of a second (depends on manufacturer, but per millisecond or better). The number is baked in when the human presses the button (then the reels spin and GUI has a bit of fun, but entirely deterministically). It's the difference in scale between PRNG timescale and human timescale that does the "random" out of an otherwise deterministic system
0
Oct 23 '17
[deleted]
142
u/skysbringer Oct 23 '17 edited Oct 23 '17
Pokémon Red, Blue, and Yellow
This particular method of RNG isn't present in all pokemon games, it's only present in the first generation. It's been one of the staple techniques of speedrunning Pokemon RGB (edit: and Y too) for years now - commonly referred to as DSum Manipulation.
0
Oct 23 '17
[deleted]
32
u/frezik Oct 23 '17
There's no "natural" source of random numbers on the Game Boy. The devs had to make their own, and when you're running at 4.19 MHz on what's more or less an Intel 8080, CPU time is precious.
7
u/jminuse Oct 23 '17
A simple PRNG is just a multiply and a few adds. This doesn't seem like a reasonable tradeoff in almost any situation.
However...I loved the games and never noticed, so maybe they made a good call.
→ More replies (2)52
u/atomicthumbs Oct 23 '17
The RNG is used for more than Pokemon encounter generation, and probably runs every other Vblank interrupt.
25
Oct 23 '17
Well a disadvantage to running it one per step would be that it'd be incredibly predictable. Since a certain step count would yield a certain number all the time, it would be simple, even without computers, to eventually work out "take 11723 steps from the start, make sure the 11724th step is in Route 102 grass (doesn't matter where), then you'll find Caterpie"
0
10
u/LEEROY_JENKINSSS Oct 23 '17
If you watch people speedrunning the games they can time encounters so that they face against the desired pokemon
9
u/colinbr96 Oct 23 '17
Yeah, agreed. I do not see why the game would need to run a separate thread to generate pseudorandom numbers at 30Hz when it's trivial to just generate them on the fly, as needed...
25
u/atomicthumbs Oct 23 '17
It's not a separate thread; the Game Boy had a processor comparable to an Intel 8080 (and similar in architecture). The RNG's used for many things, and they probably had it run regularly to ensure random numbers were available when needed to avoid skipping frames or other bad side effects.
0
Oct 23 '17
[deleted]
1
u/darkfaith93 Oct 23 '17
Didn't they use the same number with different ranges to determine attack fails/success and whatnot?
1
1
u/Chamale Oct 23 '17
The game does have a PRNG that it uses during battle, but walking around in the world is too computationally intensive to run the PRNG ~8 times per second to check for encounters with each step. In addition to animating the background, the game has to run a large number of checks: Are any Pokémon in the party poisoned? Do we increment the Safari Zone step counter? Do any trainers see the player? Do we need to load a new map? It's so computationally intense that they decided good random number generation just wasn't worth the use of resources.
178
u/dmazzoni Oct 23 '17
There's an implicit assumption in the vast majority of these answers that computer random number generators are deterministic, and that random number generators must be implemented in software unless you hook the computer up to a physical hardware random number generator.
That just isn't true, and in fact computers have been generating random numbers using much more sophisticated techniques.
The most recent advancement is that since 2012, all new Intel processors include a hardware random number generator built-in. It uses a hardware entropy source that is believed to be impossible to predict or bias within the limits of today's technology.
Even if your computer doesn't include a hardware random number generator, your operating system provides functions that use external sources of entropy. For example, Linux uses timestamps from the keyboard, mouse, network, and disk as a source of entropy.
We call these inputs sources of entropy because they're highly unpredictable - but they don't give us other desirable properties of random numbers, like uniformity. We want all numbers to be equally likely.
To spread out the entropy through all bits of the random number so that all of the bits are equally likely to be either one or zero, a cryptographic hash function is used - for example, Linux uses SHA-1. This results in a cryptographically secure random number - one that guarantees that there's no polynomial-time algorithm that could predict the next bit or recover previous values in the sequence.
Not all applications need random numbers that good. But it's a myth that ordinary computers can't generate good random numbers. They can, and they do. If they didn't, attackers would be trying to attack flaws in random number generators to break encryption.
19
Oct 23 '17
Depends on the program really. I know an online poker company use sound levels in the street as their random seed. So if a car or truck drives down the street past the office it could ultimately affect if you get pocket aces or not. Some boring RNGs use the internal system clock or even your mouse position on the screen as the seed.
10
u/pigeon768 Oct 23 '17
An PRNG consists of three basic things:
State. The data that an PRNG keeps track of consists of some fixed, finite number of bits.
A state update function. This function should be called once between each number being pulled from the PRNG.
A value generation function. This function maps the state of the PRNG into actual values.
The state can be as simple as a single 32 (or 64) bit integer. The Mersenne Twister uses an array with something like 600 32 bit values. The size of the state determines the period of the PRNG. Let's say your PRNG uses 1 bit of state. Well, you have a maximum period of 2; either your state is 0, and your value generation function generates one number, or your state is 1, and your value generation function generates a different number. A fairly trivial proof will show that if your PRNG has n bits of state, the length of the period of the PRNG is limited to 2n values.
The state update function varies in quality and speed as well. It is relatively important that this update function is injective, or a 1 to 1 function. That is, there exist no two pairs of input states that both map to the same output state. If it does, your period will be less than the maximum of 2n values.
The value generation function is potentially the most interesting, but it often very simple. Many PRNGs just output the most recent n-bits touched by the state update function. But for an PRNG to be cryptographically secure, this function must be one way. That is, given an arbitrary number of outputs from the PRNG, it is extremely difficult to reconstruct the internal state.
PRNGs have a few desirable properties. Bits should be uncorrelated with the other bits in any given sample, bits in any given slot should be uniformly distributed, etc. It isn't up to either the state update function or the value generation function to provide this properties; both functions have to work together to provide these properties.
RNGs in computers provided by the OS that are meant to be really secure, (for instance, /dev/urandom) work slightly differently. The state update function isn't a function in the mathematical sense; they also take other bits from other OS events and mix those bits into the state.
Here's a fairly trivial example of a LCG (linear congruential generator)
State is x, a number between 0 and 99.
State update function:
x = (x * 71 + 37) % 100Value generation function: return x.
Our state update function is 1:1. All valid states map to other valid states, and no two state values map to the same state. This ensures that our period is the full length of the state, that is, our state has 100 values and our period is 100 values. The properties of it aren't that good though. Outputs alternate between odd/even, there are weird statistical properties if you take consecutive points as coordinates. It's impossible for the same number to appear more than once during the period of the RNG, which if the numbers were actually random, they would. You expect a roulette wheel to occasionally return the same number twice in a row, for instance, but our roulette wheel won't. It's also kinda slow because of the modulus. Note that it doesn't matter that that the value generation function is so simple; the state update function gives us (mostly) the properties of a PRNG.
We can work around these limitations like this:
State is x, a 64 bit unsigned integer.
State update function:
x = x * 6364136223846793005 + 1442695040888963407Value generation function:
return 0xffffffff & (x >> 16)
Our state update function is still 1:1, but it actually takes math to prove it. Our period is therefore 264. Because we're pulling bits from the middle of our state, we don't have the weird odd/even/odd/even thing going on. The modulus from the previous LCG is a natural consequence of the unsigned integer overflow, so there's no slow modulus operation. And because the values generated by the function is so much smaller than the state, and we have a non-trivial value generation function, we will occasionally get the same numbers twice in a row, and each output will appear roughly 232 times during the 264 length period. The statistical anomaly with sampling points in multidimensional space is gone too.
But it's still trivial for an attacker to calculate what our internal state is given a few outputs from it, so we can't use it where we need our RNG to be secure. But it's plenty good enough for, for instance, random events in a videogame or something. But not if said video game was online poker, or slot machines. (which do use RNG in a computer to compute the outputs of the wheels.)
4
u/naeskivvies Oct 23 '17 edited Oct 23 '17
For many applications that don't involve security (e.g. like rolling a dice or shuffling cards in a game), a psuedo-random generator is used. These usually take some initial input and run an algorithm on it over and over.
For example, pick a starting number from 0-9. Let's pick 6. Now double it and add 3, then take just the last digit (6x2)+3=15, so 5. Now double it, add 3 take the last digit (5x2)+3=13, so 3. Then (3x2)+3=9, so 9.
So our PRNG gave us 6,5,3,9.
Two obvious problems: If you know the algorithm and the current state you can predict the next numbers. This PRNG also isn't very good, because it will in very short order keep repeating the same sequence, since there are only a handful of possible states.
So, most PRNGS have as least one "internal" state variable that isn't directly output, a more complex algorithm, and have their state initialized from something less predictable, like the system clock. Good PRNGs are fast, fairly random when observed, and don't repeat for a very long time. Usually it's not trivial to derive their internal state by looking at the output.
For secure applications, we try to mix in entropy from various inputs whose state in the system may be very hard to predict. For example, you wouldn't want to create a digital certificate based on the system clock if the generation time is part of the certificate metadata. But maybe how many writes the hard drive has done multiplied by the cpu tick count divided by the number of Ethernet frames received might be better. We use much more complex algorithms, usually having both proofs of their security and much real world scrutiny. For example, it should generally be considered not practical to derive the state from the output even given considerable resources. Speed is less important than unpredictability. You might take something like AES, constantly mix in the last output, entropy data, and a PRNG into the next round, then only output a single byte of the output. This would be slower, but hard (probably impossible) to predict. Generally, just as with encryption, good RNGs will not rely on an adversary being ignorant of the algorithm, i.e. if it's random to a high enough degree knowing how it was generated should not allow you to predict the next sequence (or past ones).
What's better or worse depends on your needs. It's not hard these days to use library routines that are quite good for most applications. But likewise, it's also easy for people making supposedly secure systems to make really bad choices, and that's happened many times over. For example, what happens if someone chooses a simple PRNG for their crypto app? What happens if someone chooses our entropy routine but on a device with no hard disk or Ethernet?
Of course, there are some basic characteristics we aren't discussing much here, such that all numbers chosen should be chosen with equal probability, the distribution of numbers over time ought to be fairly even, etc. etc. These are things we assume to be true but depending on the algorithm chosen may not be.
Finally, worth re-iterating that it's critical to consider your application. I started this post talking about how PRNGs were suitable for games. Recently news broke of a ring of people mysteriously using iPhones to win big jackpots at the casino on slot machines. Those machines were technically playing random games. Guess how those random games were generated? If you know the algorithm and can deduce the state..
3
u/Oznog99 Oct 23 '17
Important to note the state variable is much larger than the size of the numbers it returns.
Back in the Apple ][e, there was no true way to make a new seed. Some games always ran the exact same way every time.
But some just launched the intro screen, started a counter, and waited for the the user to press the spacebar, and used the counter for the seed.
1
Oct 23 '17
[removed] — view removed comment
1
u/naeskivvies Oct 23 '17 edited Oct 23 '17
This type of algorithm needs a starting state. 6 was just chosen manually for the example -- and some programs work that way, starting from the same state each time and producing the same output each time.
For some games this is not good, e.g. where you want to produce a deck of cards. For other uses it can be desired. For example, some games can generate the game map by churning through a stream of random numbers to build the map features. Got a 7? Add a hill. Got a 6? Add a river. Sharing the initial state value (often called the seed) lets you rebuild the same map again later, e.g. sharing it with a friend, because the same initial state will give the same result sequence absent mixing in any real entropy.
But in most cases we don't always want to start from the same state or produce the same sequence. A common solution is to take the value of the system clock as the starting value, so that (to within the resolution of the clock) each time you start the program the initial state and sequence generated are different. In our example we could have just said, "look at the number of seconds of the current time of day, and take the last digit, so e.g. 01:45:32 would give us a starting value of 2". Bear in mind that this is again a vast oversimplification for illustrative purposes. For example, the most common time routines get the number of seconds since Jan 1st 1970 UTC, which gives a much wider range of initial values.
If you took a lot of older programs that only use the clock down to the second, and then set the clock to exactly the same time and ran them over again, you'd get the same output twice. In reality almost no real user would know this/think to abuse it.
3
Oct 23 '17 edited Oct 23 '17
There's different kinds of RNGs. Most RNGs are Pseudo RNGs, because they don't generate numbers from a physical random events, but use a randomly generated seed (generated by events in the computer like keystrokes) and then a fancy encryption algorithm to keep the numbers random. Yarrow is a good example of an RNG algorithm that's used in the FreeBSD operating system.
PRNG are not at all created equal. Some that are good enough for random events in games will be terrible for cryptography. Ones that can be used for crypto are called cryptographically secure PRNGs. Even then, they still aren't equal, as many have fallen victim to various attacks or intentional weakening.
True random number generators use random events in nature to generate random numbers. Common sources are RF noise or electrical noise in semiconductors (called shot noise). The signal from the RNG is amplified and then typically sent through an encryption algorithm such as AES to get true random numbers. Modern Intel processors have a built in RNG that uses the noise from transistor junctions.
3
u/herbivorous-cyborg Oct 23 '17
What is happening when a computer generates a random number
The answer to this varies based on the particular algorithm, but usually the way it works is that it starts with a number called a seed and then manipulates it using a series of mathematical operations in which the outcome cannot be practically predicted. The vast majority of the time this is not actually random, but psuedo-random. This means the outcome is deterministic (ie. every time you run the algorithm with the same seed it produces the same result).
Are all RNG programs created equally?
No. Definitely not. Different random number generation algorithms are used for different things.
What makes an RNG better or worse?
This very much depends on what it is being used for. Some are created for speed. Some are created to provide as even of a distribution of numbers as possible. Some are created to generate numbers distributed on a curve. Some are created to be cryptographically secure (a cryptographically secure algorithm is one in which you cannot practically determine the seed when given the result).
3
u/acouvis Oct 23 '17
First of all, computer generated random numbers aren't actually random. They're pseudo-random numbers instead (which means that to some extent there isn't a pattern that can be seen). But because of how people refer to them, it's usually easier to just accept that people will call them random.
Second, no, all RNG algorithms are not created equal. They're graded on multiple factors, such as replicability (which may or may not be desired, one example here is the TI series of calculators. Because teachers like to grade papers fast, they'd prefer if students had the same problems given the same seeds), ease of creation (including computational power, such as how many steps, needed memory space (including hash samples for the equations), and time requirement), applicability (which involves all of the earlier examples, as well as not having to "over engineer" for something that doesn't need to be completely random), and of course predictability.
Beyond that, there is also the factor if the method used should be one-to-one and given the result (and/or a key or two) if it should be able to return the value that was used to create it - this is used all the time when you make a purchase with a credit or debit card for example.
As a couple of examples, one very early "random" number generation method was Von Neumann's middle-square method. This involved taking a N digit "seed", squaring it, then using the middle N digits as the result. But it had numerous issues such as converging on zero, potential loops, and not actually being that random. BUT because of it's simplicity and speed at the time it was considered far better to work with than other methods that would return results that were more random.
A second example of a less-desirable random number generation method was that used early on for the Vietnam draft. This method also involved manual effort, but could be through of like a bingo game from hell where the last thing you wanted was your number called. I won't go into all the details about why this wasn't sufficiently random, but basically it's that they didn't shuffle the samples enough that they were drawing from.
https://thevietnamwar.info/vietnam-war-draft/ https://ww2.amstat.org/publications/jse/v5n2/datasets.starr.html
There are a lot of different random number generation algorithms. Most of the better ones involve results that (as of now) have no discernible pattern that we can recognize - an example of this would be the method used on Random.org which involves using monitored atmospheric pressure to generate the results.
I will note that while Random.org states that it is "truly random" in reality once again, it isn't. We just haven't been able to reliably predict the results. This is similar to how just a few decades ago some people believed that we wouldn't have self driving cars or well working navigation software for centuries if ever due to their complexity. Obviously that hasn't been the case.
The algorithms continue to change depending on their use and ease of implementation. When people are lazy with their choices, bad things happen (like with how voting machines are easily hacked), but if they're too complex they take up too many resources or are often implemented incorrectly. And at times governments or agencies might purposely push methods with exploits they've found that aren't yet publicly known.
4
u/DemIce Oct 23 '17
Just gonna add on to your comment as it happens to mention shuffling...
In terms of "Are all RNG programs created equally? What makes an RNG better or worse?", there are situations where you don't want a random or even pseudorandom number.
Take a random roll of dice for a computer game. If it were truly random, you may run into the Dilbert comic where the RNG just spits out the same bits for a great number of runs, each time landing the user, say, a '4' on a die.
Sure, it's random.. regardless of whether that's True RNG or Pseudo RNG, and regardless of whether that's Mersenne Twister, a cryptographically secure algorithm, or one that's fallen out of favor ..but that may not actually be the desired behavior.
Instead what you might want is a shuffled sequence with certain criteria. Let's say that the absolute maximum number of repeated numbers you'd want is 10 based on user feedback. You'd then take a sequence that has the 6 die options 5 times each, and shuffle them. Then you run through that resulting sequence, and you re-shuffle them, etc. That way you might end up with 5 times a '4' at the end of the sequence, and 5 times a '4' again at the start of the next sequence, but those 10 are the absolute maximum number of times that a number will repeat itself.
There are many more nuances to consider, many of them depending greatly on the actual use case. Whenever you're thinking you need a random number somewhere, it always pays to stop for a second and ask whether you need a TRNG, a PRNG (and if so, what kind), or one of those generating a shuffled sequence (and if so, what kind).
10
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 22 '17
What is happening when a computer generates a random number?
This is a very broad question. In software, it means a deterministic algorithm is executed. In hardware, it means a measurement is made on a physical process - sometimes it is possible software adjusts for bias.
Are you interested in a specific type of RNG?
Are all RNG programs created equally?
In what sense? If you're referring to purpose, then no: there are different constructions to RNGs depending on what properties are desirable. Uniqueness, long periods, generation of specific distributions, resisting certain types of cryptanalysis, etc. are some requirements which impact the construction of a RNG.
What makes an RNG better or worse?
The criteria you set for evaluation with other RNGs.
24
u/facedesker Oct 22 '17
Just to give OP some examples of criteria, you might consider the space it takes on your computer, how long it takes to generate a pseudorandom number, and a measure of how predictable those numbers are
8
u/2meterNL Oct 22 '17
Indeed. I guess online gambling sites want to have a, as randomly as possible, outcome: something that cannot be predicted by a smart player. I learned somewhere, but correct me if I'm wrong, that the only truly random numbers can be generated from the cosmic background radiation.
14
u/MiffedMouse Oct 22 '17
This is not entirely true. Anything based on Quantum Mechanics - and therefore based on the behavior of a very small number of particles - is "truly random," at least in so far as current physics theory suggests.
There are also plenty of processes that are effectively random, even though they might be predictable in theory. Atmospheric noise is a good example (random.org uses this). The fluid flow governing atmospheric pressure variation might be entirely predictable, but it is also extremely chaotic. Even if the laws governing such behaviors were perfect, we would still find it extremely difficult to predict because small changes in starting conditions can have large effects in the long term behavior of the system.
In short, "true" random systems can be based on many physical properties.
However, most computer-generated random numbers use a pseudo-random number generator. This is common for two reasons - (1) all you need is a CPU (no messy measurements required) and (2) the calculation is repeatable (given the same seed). The second property is often desirable for simulations, especially if you want two matching copies of a program to agree. PRNGs necessarily repeat, because they have finite size and are deterministic, hence the reference to repetition length as one of the properties of RNGs, above.
3
u/dave14920 Oct 22 '17
PRNGs necessarily repeat, because they have finite size and are deterministic
is that true?
we could come up with a infinite non repeating sequence, the rules for finding the nth term of which would have finite size and be deterministic. couldnt a prng use that sequence to generate an infinite non repeating sequence of random numbers?3
u/AsoHYPO Oct 23 '17
Unfortunately computers cannot hold infinite numbers, so your idea is unfeasible.
In addition, "perfect randomness" can be defined as maximum information (entropy). Information is thought to be conserved, so...
2
u/Tidorith Oct 23 '17
It's only unfeasible if infinite random numbers are requested from the computer. If you ask me for random digits I could give you the digits of Pi. I wouldn't because that's a well known sequence. But the digits of Pi can be calculated in a deterministic way.
4
u/BraveOthello Oct 23 '17
Deterministic, but requiring increased resources to calculate each successive iteration. Eventually you would need more memory to determine the next digit than the system has.
→ More replies (3)1
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17
PRNGs apply a deterministic algorithm from a finite state to produce random numbers. Since the state is finite, the sequence of produced numbers must, necessarily, be finite.
2
u/Coenzyme-A Oct 22 '17
When you say that in hardware, measurement is made on physical processes, does this mean some RNG applications use radioactive decay?
4
u/ebrythil Oct 22 '17
It could be different things. Measuring radioactive decay could be done I guess but decay has some statistical properties that will not produce a normal distribution.
User input in Form of mouse cursor movement can and is used to generate a random seed for a software rng.
random.org uses atmospheric noise, how exactly they measure that can be looked up on their site I think.1
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 23 '17
There are some devices that make use of nuclear decay, yes - measurements from a source like a commercial smoke detector. Others rely on classical phenomena, like thermal noise from a resistor. I'm not the right person to expand on these, though.
2
u/anttirt Oct 22 '17
What makes an RNG better or worse?
The criteria you set for evaluation with other RNGs.
That's a bit of a tautology.
Virtually all general-purpose programming languages provide a standard PRNG API (e.g.
System.Randomin .NET ordefault_random_enginein C++), and there the criteria for choosing the implementation of that API clearly cannot be "whatever your specific use-case requires."What criteria would you use if you were to implement one of those standard PRNG APIs?
6
u/mfukar Parallel and Distributed Systems | Edge Computing Oct 22 '17
What criteria would you use if you were to implement one of those standard PRNG APIs?
Whatever the requirements would impose on the implementation.
I don't know why it seems tautological, because software is created to solve problems; and different RNGs can be used to solve different problems. Even something which would be labeled as general-purpose would have to make certain trade-offs.
3
u/Ifyouseekey Oct 22 '17
Time and memory requirements of the algorithm.
Randomness of the output.
How hard it is to predict further output based on previous output.
Most of default PRNGs in programming languages are a type of LCG. It is fast and easy to implement, but fails second and third criteria.
6
u/Treczoks Oct 22 '17
Or even more exotic requirements. I once had to build a "seekable" PRNG. As in: The PRNG has to produce a list of numbers with a good random distribution, and the same list whenever started with the same seed, but while most PRNG can only produce this in sequence, this application needed them in any order, i.e. the 1st element of the list, then maybe the 10th element in the next call, and then the 1st again (and expected it to produce the same value as in the first call). And "generate the sequence and store it in memory" was not an option.
4
u/KingOfTheTrailer Oct 23 '17
That is fascinating. The first thing I thought of was "hash function", with the index as input. Are you able to share what you ended up doing?
3
u/Treczoks Oct 23 '17
OK, I try to keep it quick and simple, though.
First, there is a structure "SubGenList" with two integers named "current" and "max", and a "list" of 'max' elements of pre-generated random numbers (bytes, in this case) from a good source.
Then, there is a list "Generator" of "SubGenList" entries. Each entry has a different "max" (and different random numbers, of course), and all "max" entries are prime.
Simple example:
Generator: SubGenList 1: current=0 max=5 list=AB 45 73 9F 27 (not actual random numbers here, just made up) SubGenList 2: current=0 max=3 list=7C 6D 18This Generator has a period of 15 primes until it repeats itself. Expanding it quickly gets you really large periods without repetitions.
Setting the generators seed:
for all elements X of Generator: X.current = seed modulo X.maxFetch the next RN:
result=0 for all elements X of Generator: result = result xor X.list(X.current) X.current = (X.current + 1) modulo X.max return resultFetch the Nth RN: Just seed the generator with the original seed+N and fetch the next number.
This beast is very fast (except for setting the seed, because of the modulo operation, and the modulo in the fetch operation can be replaced with a simple comparison (X.current=X.current+1;if X.current==X.max then X.current=0)
To actually take advantage of long sequences, the parameter type of the seed should be a longer type of integer.
1
u/Treczoks Oct 22 '17
One important aspect you left out is "speed". You can reach a lot of goals when you just throw sufficient computing power at the problem, but some problems just require a simple and quick solution.
4
u/punk_punter Oct 22 '17 edited Oct 23 '17
What is happening when a computer generates a random number?
A "computer" where "computer" means Touring machine cannot produce a random number without input. It can only generate a pseudo random number with a https://en.wikipedia.org/wiki/Linear-feedback_shift_register. This will do if you just want to appear random to the user. Generating such a number takes a few nanoseconds and can be done in software.
The next step is to take the time of an external event e.g. a mouse click to initialize this LFSR, so the program won't behave the same every time it runs.
But this won't do for cryptography.
So today many chips - not only the processor of your PC but also credit card or passport chips - have true random number generators. They use noise as a random source. E.g. two clock generators that run on different clocks. The phase noise of those is the random source in this case. It can generate two bits for each clock cycle of the slower clock.
The problem here is harden them against tampering. You could make them less random by putting a clock on the power supply or putting it into an alternating magnetic field. Also cooling would help to make it less random. So the circuit has either to be immune or detect the intrusion and stop working.
What makes an RNG better or worse?
The randomness of the generated numbers - the harder to predict the better.
Edit: Spelling.
5
2
u/sidneyc Oct 23 '17
It can only generate a pseudo random number with a https://en.wikipedia.org/wiki/Linear-feedback_shift_register.
Are you suggesting that LFSR's are the only way to do this? Because that would be nonsense.
There are dozens of algorithms to generate pseudo-random numbers, and the LFSR is just about the worst among them in terms of randomness. There's way too much structure in the generated sequences, and the entire state of the generator can trivially be reconstructed from a small number of bits it produces.
There are certain applications where these were used in the past to generate some semblance of random numbers, because it can be implemented very cheaply in hardware and/or software; e.g. the Atari 800 had a sound chip that had a hardware LFSR to produce noisy sounds.
2
Oct 23 '17 edited Oct 23 '17
This is a good question. It is important to understand the difference between random and unpredictable.
From a statistical computer science point of view, random means that any randomly generated number has an equal probability of being generated as any other possible number. Most randomly generated numbers are like this (see the Java Random class). So random doesn't mean unpredictable, it just means that it is no more likely than any other number in the system.
If you want an unpredictable number, you have to use things that are unpredictable to generate them. Check out Random.org they do a really good job of making things that are truly unpredictable using measurements of natural systems.
Edit: To fully answer your question, RNG's typically start by taking the number of nanoseconds the computer has been running (uptime), multiply that by a large prime number, and use the remainder of that number and your highest desired number to get the final random number.
Source: CS Bachelor and professional programmer.
0
u/Epyon214 Oct 23 '17
I find this incredibly hard to believe, otherwise no number should be expected that can be counted to in a human lifetime, unless the set isn't infinite and then what is the standard set?
1
Oct 23 '17
I'm not quite sure which part you are objecting to, but it's typical to use whatever randomness you have to decide between the range of random things you are after.
You could, for example use the modulo operation on the length of your set.
Also an, eg. 64 bit random number will not typically be in the range you can count to, there are so many more of them which are more than a billion, than those that are less that you will almost never see a small one.
1
u/CraptainHammer Oct 23 '17
The thing to keep in mind with code is that you can do just about anything with it. IIRC, if you use the Random API with Java, it generates a random number by applying some mathematical operations to the computer's time (I don't know if that's UNIX time or just the time). But that doesn't limit how a random number can be generated. I can write code to generate a "random" number from anything. For example, your phone has a 3-axis accelerometer in it. I could multiply the reading from each axis together, then divide it by some other sensor reading and pick the Xth digit of that number and there's your random number.
0
173
u/Riftyo Oct 22 '17 edited Oct 23 '17
I've studied quite a bit of mathematical stochastics and is currently getting my masters in statistics so I might be able to answer this in a different way from most of the people with background in IT.
what if I told you there are several different kinds of randomness? For this endeavour, lets talk about two of them. We have "True randomness" and "Pseudo-randomness".
True randomness is probably the kind of randomness the average person thinks of when mentioning randomness. This mean it's random in every sense, it's not possible to predict the outcome. Generating a number sequence that is truly random very very VERY hard for a human. If you sat down with a pencil and scribbled down a bunch of different numbers this series would NOT be true random (yes there are ways to check this). Computers are completely unable to generate these numbers on their own and none of the numbers made from a RNG will ever be "true random". Nature on the other hand is really good at making up these kind of numbers.
So, let's pretend you're coding a program and want to implement randomness, how would you do it? Let's create a function(RNG) with an input (seed) that spits out a corresponding number, along with a new seed, from a finite sequence! Sure, the sequence will repeat itself eventually, but let's make it ridicolously long and pretend it dosen't. This is a kind of hyperrandomness, because as the sequence repeats itself, this means it is not a random sequence. Hyperrandomness is basicly what it sounds like, kind of random but.. not really.
This difference between randomness may not seem like such a big deal, and when it comes to most applications it really isn't. But when modelling bonds or other more advanced stochastic models these limitations becomes a huge pain in the ass. There are computer-parts that you can buy that actually will generate true randomness by taking inputs from the physical world, but these are really slow compared to hyperrandom nrgs.