Книга: A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 2 (for True Epub)
Назад: Generating Random Numbers
Дальше: The Fisher-Yates Shuffle

TRNGs vs. PRNGs

Technology decisions are all about trade-offs, as I’ve mentioned, and both TRNGs and PRNGs have their pros and cons. The major advantage of TRNGs is that their numbers are much closer to being truly random; that is, the numbers are way less predictable. (Can you predict what the next lava lamp formation is going to look like?) PRNG numbers, on the other hand, are completely predictable once you know which formula is being used to fuel the PRNG.

Sometimes this difference is critical, as there are numerous applications where only truly random numbers will do. One of the most prominent examples is cryptography, which relies on the random generation of passwords and secret keys. For example, when we sign up for a new online service, our Internet browser asks us if it should generate a random password for us. If these passwords were only pseudorandom and generated using a predictable pattern, hackers would have a heyday breaking into people’s accounts.

Another example is casino software. If a smart person could predict the combination of cards that the electronic poker machine will deal out next, the casino would go out of business fast. Even if something like Mersenne Twister is being used under the hood, a hacker may be able to find out the precise random number previously generated, and thereby be able to predict the next one.

For applications like these, PRNGs like Mersenne Twister aren’t up to snuff. In the words of Python’s documentation of the random module, “The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.”

On the other hand, for applications that don’t absolutely need “true” random numbers, PRNGs have distinct advantages. First, PRNGs are practical; no radioactive material is needed. Secondly, PRNGs are generally faster, as a computer only needs to make an internal mathematical computation.

So much more can be said regarding generating random numbers, but it’s time to get back to the topic of randomized algorithms. In particular, let’s look at how to figure out the time complexity of shuffling an array.

Назад: Generating Random Numbers
Дальше: The Fisher-Yates Shuffle