Monday, 27 August 2012

Random Number Generators

Randomness is a common ingredient in computer programming, sometimes for functions you may not become aware of as a user, and sometimes for visible effects. My Android apps use randomness, partly for comic effect, partly to create a more dynamic user experience and partly to stimulate creativity, as I explored in this article: Using Randomness To Stimulate Creativity.

Anyway, the project got me thinking about randomness generally, as it's a fascinating topic. In this article I'll go through the basics of how randomness and random numbers are involved in computer programs.

Uses


Many computer programs use randomness to create dynamic, unpredictable effects. These programs typically use random number generators, which are provided as standard within most of the major languages for desktop and Web programming. For example, a computer game could use a random number generator to choose numbers between 1 and 6 to simulate a dice throw. Shuffle functions in media players sometimes also use random numbers to choose songs and implement play-lists.

The field of security in computer technology also often involves random number generation, particularly when encrypting data.

Random functions within Web development implement various tasks, such as presenting the user with randomly select content from a data source each time they visit a site. This allows developers to maximise on the amount of content they have for a particular Web project. Sites referred to as Web 2.0 often use these functions, particularly when they contain interactive features such as polls.

Random numbers are also often used to prevent caching issues in Web development projects. For example, sites use AJAX functions to fetch new data from a server while the user is viewing a page. In these sites, JavaScript functions within the Web page call PHP or other server side scripts running on the server, which respond with new data, sometimes from a database and formatted in XML. The scripts then write this new data into the Web page. However, developers often encounter problems with caching, in which the user's browser fails to update the content because it has cached it. By passing a random number to the AJAX functions, developers can avoid these caching issues.

Random Number Generators

Programming languages provide standard classes, functions and utilities for random number generation, allowing programmers to generate random values within particular ranges. The first thing to understand about these utilities is that they do not actually generate numbers that are truly random.

By definition, it's not possible to create a computing algorithm, or process, that has random results. Instead, random number generators produce pseudo-random values. Some random number generators are better than others, but in general they aim to produce sequences of numbers that appear random to human observers. The random numbers are normally produced using arithmetic calculations, but most programmers can use them without understanding anything about the underlying process.

Programmers sometimes also need to generate series of non-repeating random numbers. For example, in a typical series of random numbers within a limited range, for example between 1 and 10, you will see the same numbers appearing more than once. To create a series of random numbers without duplicates, programmers need to store the numbers generated in a data structure such as an array, checking each new value against the existing values before accepting the new one into the sequence. These programming activities typically involve control structures such as loops and conditional statements.

Issues

As mentioned above, some random number generators are better than others. Anyone who has tried using them in programming tasks will be well aware of how frustratingly repetitive they can be. As a user, if you've ever felt your music player was repeating the same songs all the time, you might have been right!

Alternatives


The only way to generate truly random numbers is by taking them from naturally random data, which is what services such as the Random.org site do. You can use it to choose lottery numbers, toss coins and pick cards in a more realistic way than most computer programs can produce.

However, for most computer programming purposes a pseudo-random number generator is sufficient and relatively easy to use.

Related Resources:

No comments:

Post a Comment