# The nature of randomness on computers

## Examples of random number functions

Pretty much every language that you'll encounter will include some form of random number generator. The syntax differs from language to language, but it's all pretty similar. Below are some examples of grabbing a random number in the range 0-10:

MEL

```int \$myNum = rand(10);
```

MEL has a simple random number function that requires a single input- the maximum value to be returned. The output will be between 0 and the input.

Python

```myNum = random(0,10)
```

Python, like many languages, allows you to specify both a start and an end range for the random values. The lower number is a hard limit, while the upper one is a soft one. That means that you will get values that are greater than the lower number and less than or equal to the larger one.

C++

```int myNum = (rand() % 10) + 1;
```

In C++, things are a little bit more complex. The rand function returns a random value in a large range of integers. If you want a specific range, you can approach it one of two ways. The easiest way is to use the modulo operator. In the above case, that will produce a value in the range 0-9, so one is added to move the range over to 1-10.

## No such thing as randomness on computers

Okay, so this is important. There is no such thing as true random numbers on a computer. Period. Computers (as we know them) are deterministic machines. That means that there is no chance whatsoever that a given function or process will produce a random number.

But games are random, right? True. There are lots of examples, in gaming, cryptography, and many other fields of computer science, that utilize numbers that seem random. But they only seem that way. In fact, every random number you've ever bumped into on a computer (unless you've been using some extremely special purpose hardware) was the output of a psuedo-random number generator.

Psuedorandom number generators are just that- psuedo random. They're random enough, if you back up, squint your eyes a bit, and ignore the man behind the curtain. Randomness on a computer doesn't mean that the number sequence is random (i.e. that it is completely unpredictable from one moment to the next) but rather that the numbers in the sequence tend to move around a lot. A good psuedorandom number generator produces a sequence that has no easily-recognizable pattern, while a badly implemented one has patterns that stand out in obvious ways. However, all random number generators that you'll use are completely deterministic.

Most psuedorandom generators start with a given input number which forms the first number in the sequence. They then perform some number of completely deterministic mathematical operations on that number to produce the next number in the sequence. The new number is put through the same process to generate the third, and so on and so forth. A typical approach is the linear congruential method.

That first number is, as you might imagine, very important. It is referred to as the "seed" value, since it gives rise to a particular (and potentially huge) list of numbers, just as a seed gives rise to a particular (and potentially big) plant. If you start with the same seed value and call the random function the same number of times, you will get the exact same sequence of values every time. Most languages have separate functions for setting the seed value for their internal random number generators. Those functions are often called srand, for "seed random".

You can get a bit tricky with it, though. A really good way to introduce variation is to use the system clock (usually the number of milliseconds since Dec 31st, 1970) as the seed value. That way, each time the program or script is invoked, it will start with a different seed value, thus producing a different sequence of numbers.

It might seem that the lack of true random numbers is a big limitation, and it is for some problem domains (cryptography, for example). However, for most of the things that a technical artist encounters, it doesn't really matter. Furthermore, the deterministic nature of pseudorandom numbers can actually work in your favor, sometimes with huge benefits (see below).