Suite101

Programming With Random Numbers

Using Pseudorandom Numbers in Real Programming Projects

© Guy Lecky-Thompson

A tutorial for using random and pseudorandom numbers when programming requires that the resulting number sequence does not repeat itself, with real examples from games.

Pseudorandom Numbers

A pseudorandom number sequence is quite simply a sequence (or series) of numbers that is unpredictable. In other words, it is not possible (or very difficult) to perceive a pattern in the numbers from the sequence presented, by simple observation. To all extent and purposes, it is random : as if each number in the sequence is chosen by chance.

Most, if not all, programming languages contain a random number generating function. Usually, it needs to be 'seeded' on a given value which provides the starting point for the series of numbers. This is not the place for a complete dissection of available algorithms, but it is worth mentioning that there are several, and that each one aims to guarantee a sequence of numbers that only repeats itself after a given number of iterations.

For those wanting to run down the list and see how each works, there is an excellent list here : List of Random Number Generators. All that is really required is the knowledge that a specific function can be seeded, and then called, to produce a number between 0 and an arbitrary maximum.

Random Number Functions

In ISO C, the two most useful functions are:

  • srand(seed);
  • rand();

If rand() is called without first calling srand(seed), then it is assumed that the sequence to be generated is the equivalent of first calling srand with a value of 1. This is reasonably standard practice for random number generators.

In Visual Basic, the equivalent functions are:

  • Randomize seed : srand(seed);
  • Rnd : rand();

In order to seed before use, the Rnd function should be called with a value of -1, before calling Randomize with a specific seed.

The point of being able to obtain a repeatable sequence of unpredictable numbers (seeded sequence) is twofold. Firstly, when testing a program, it is useful to know that the same sequence is being generated so that debugging is easier. Secondly, if the sequence is being used to generate landscapes, in a computer game, it is often desirable that everyone has the same landscape, although it should not be based on a pattern, but random.

Non Repeating Sequences

However, random numbers generated in the above fashion are not useful for sorting a stack of cards. Assuming a stack of 13 cards, if they should be extracted in a random fashion, the following code might seem to be appropriate:

  • card = rand() % 13; // between 0 and 12

The card could then be accessed through card_array[card]. However, the random number generator could feasibly generate the same number twice, which makes a nonsense of selecting cards : there are not 2 of each in a deck.

Subsequently, the process is a little different. To overcome the fact that the numbers might repeat (albeit unpredictably), it is necessary to use the number generator as a decision tool. In the case of the stack of cards, all that is needed is to pick a number between 0 and the number of cards left, and call that the split point.

Then, a number between 1 and 10 can be chosen, and used to select either the card above, or below the split. In this way the sequence of numbers is disassociated from the number of items left in the stack.


The copyright of the article Programming With Random Numbers in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Programming With Random Numbers in print or online must be granted by the author in writing.





Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo