Follow TV Tropes

Following

Media Notes / Random Number Generator

Go To

import random

print(random.randint(0, 99))

In Tabletop Games (such as Dungeons & Dragons), events and their magnitude can be randomized by a simple throw of the dice; Video Games use a similar mechanism which is usually much less transparent to the user. As these rolls are hidden, a common form of Fake Difficulty is to have the random number generator roll in favour of a computer player more often than it does for you.

For example, Role Playing Games will commonly use a random number generator to determine if a blow landed in combat. If so, the random number generator might then be called again to determine if it is a Critical Hit or not; then yet again to determine what percentage of the character's maximum attack power will be applied to the enemy.

Note that almost all computer systems are incapable of producing truly "random" numbers on their own. Some have special hardware which can achieve this, but you'd be hard-pressed to find such hardware in a home computer. As such, the random number generator is usually producing a series of numbers based on an initial "seed" value. This "seed" is assumed to be "truly" random, and often will be the time of day (down to the microsecond) when the program started. Which of course is everything but random.

One convenient result is that the seed can be reused when you want the exact same sequence of random numbers again. This is useful for things like recorded gameplay and procedurally generated content.

This works rather well with traditional computer systems, where the time will be different every time you start a program (making it very difficult for the user to predict the seed used, especially if a program re-picks one every time it calls the random number generator), but older video game consoles didn't know the time of day. So they had to use certain tricks.

One method commonly employed was to start a timer when the console powered up, then grab the current value from that as required. A variant is where the game might start an internal timer when a level loads and use this as a seed, though this has a disadvantage in that if you take an action quickly you can end up with a deterministic result. In practice, this isn't likely to be an issue. Another method was to modify the current random value by a number based on the controller input each frame. This would appear random to the user, since the limits of human precision prevent manipulating this (for example, Microsoft's XInput API uses ~65K discrete values for both the X and Y axis of a controller's analog stick). However, through Emulation, one can actually determine the algorithm in question by reverse-engineering and then provide the precise controller input to get whatever random number you want. In tool-assisted speed running, this is known as "luck manipulation".

Some games look to other sources for a seed value. For example, the GBA game Golden Sun based its random drops on the enemies you fought, how your party was equipped, the turn order throughout the battle, and so on. That is to say, if a player managed to win an item as a random drop using certain battle tactics – be it Shop Fodder or the Infinity +1 Sword – then repeating the battle against the same monsters with the same tactics would be guaranteed to give the same reward. This made the random number generator far easier to exploit than one using the timer as a seed.

Whether a video game using a timer-based random number generator is more "random" than a real set of dice is debatable. In practise, so long as the program is using a fresh seed every time it starts, and the player doesn't know what that seed is ahead of time, there should be no way to consistently predicting the outcome of a decent-coded random number generator. If the seed is predictable, then the results of the random number generator are, too.

On the other hand, it could be argued that a sufficiently skilled player could roll dice in a manner that would guarantee certain results... It is, after all, simple physics that determines which way up they'll face. Regardless, it's very difficult to determine the outcome of a roll before making one, and so dice rolls are usually assumed to be "truly" random. In practise, which is more "random" usually boils down to how many opportunities there are to cheat. It's also commonly argued that all values are hand-picked by the Random Number God anyway, hence rendering the mechanics moot.

In any modern well written program, the "random" number is generally random enough. Only in the case of encryption (where massive computer power can be harnessed to discover and exploit the tiniest flaw) would there be any problem. Programmers who have the knowledge and desire to do so can ensure that any computer game has all the randomness it needs. On the other hand, it is trivially easy to write a random number generator wrong, and it was done on several occasions. The best known (and most reviled) of them is the infamous IBM-designed RANDU, which failed even the most relaxed definition of the RNG (such as that the numbers it generates must be spread uniformly over the range, which they weren't). Unfortunately, due to the popularity of the IBM hardware and software that were supplied with itnote , it was the most widespread RNG of The '60s and The '70s, and even now a lot of scientific results in computing are suspect because it was used to get them.

The modern standard for pseudorandom number generators (PRNGs) is the Mersenne Twister, which passes numerous rigorous tests of statistical randomness and is the default algorithm for a variety of programming languages (such as PHP, Python, and Ruby). Cryptographically secure RNGs are vastly more difficult to build since even if they draw random values from a physical source, they could be compromised by someone tampering with or monitoring that source. Consequently, CSRNGs typically gather entropy (random information) from a variety of sources, combine it, then use that as a seed for a PRNG.

Top