Random Numbers on a computer are not really random. They are a sequence of "pseudo" random numbers.
First off, it is not really possible (nor desirable) to have real random numbers. What we want is a repeatable sequence of seemingly random numbers that satisfy certain properties, such as the average value of a list of random numbers between say, 0 and 1000, should be 500. Other properties, such as no (predictable) relation between the current random number and the next random number is desireable.
In any program where random values are necessary (for example, most simulations) the programming language must provide us with a random number generator. This will be a function that will, when called, provide us with a single random number... when called again, provide us with another random number.
Generating a single random number is easy. 27. There!
Generating a sequence of random numbers is quite difficult because we want certain assumptions about this sequence to be true!
Note that "Random" number generators produce what are called "pseudo" random numbers, meaning that the numbers only "approximate" real randomness.
Some Goals:
Some Possible Techniques to Create Random Sequence:
The advantage of using mathematics to generate a random number (sequence) is that it is REPEATABLE. This is important for the following reason: debugging a program. Imagine the problems you already have finding errors in your code. What would happen if the "path" (or program flow) was different (random) every time? Debugging would be a nightmare! At the end of the day, the programmer wants the same sequence of random numbers every time, but the user may require a different sequence. To generate a different sequence of random numbers we use a "seeding" function. For the purposes of this course, you will most likley not need to "seed" your random number generator.
In the case of Matlab and C, this generator is the "rand()" function. In the case of Java or Actionscript there is a random function associated with the Math library.
Matlabs random number generation function is called rand. In Matlab, the rand function returns a floating point number between 0 and 1 (e.g., .01, .884, .123, etc).
To create a boolean value (true/false) for a flip of a coin!p
fifty_fifty_decision = (rand() > .5);
In C rand() returns a value between 0 and a large integer (called by the name "RAND_MAX" found in stdlib.h).
To convert from the C format to the Matlab format, in C we would say: "rand() / (float)RAND_MAX".
To create a boolean value (true/false) for a flip of a coin!
#include <stdlib>
...
float between_0_and_1;
bool fifty_fifty_decision;
between_0_and_1 = rand() / RAND_MAX;
fifty_fifty_decision = (between_0_and_1 > .5);
The Math.random() function generates a value greater than or equal to 0 and less than 1.0.
To create a boolean value (true/false) for a flip of a coin!
var fifty_fifty_decision:Boolean = (Math.random() > .5);
To create a number between 1 and 10 we would do:
var random_number:int = Math.ceil((Math.random() * 10));
Below are examples of generating values using the random number generator.
For conciseness, these notes use a rand function that computes a random number between 0 and (less than) 1. If the function you are using does not produce a number between 0 and 1, you should be able to convert your number generator to this format by dividing by a large integer.
Given a random number between 0 and 1, it is relatively easy to generate random numbers or values of any type. For example:
value = rand() * 100;
decision = (rand() > .5);
x = (rand() * 50) + 50;
To get an integer from a floating point value we can use functions such as round or ceil or floor. The round function returns the nearest integer, ceil the next higher integer, and floor, the next lower integer. Thus ceil of 1.001 is 2 and ceil of 1.999 is also 2. round of 1.001 is 1 and round of 1.999 is 2. The Floor of 1.999 is 1, etc. The ceil of 1.9 is 2, as is the ceil of 1.1 or 1.0001.
x = ceil(rand() * 10);
It is unlikely that you will ever have to write your own random number generator. Most/all languages you will ever use will already have one written for you. That being said, there is some value in knowing some possible ways that numbers are generated. This value lies in knowing what to expect when you get a sequence of random numbers... You wouldn't want your Pac-Man ghosts to always drift toward the upper right of the screen, which could happen of the random numbers they used to move were not so random...
The following idea is simple, fast, effective random number generator, but one very sensitive to the choice of constants:
next_number = (previous_number * Constant1) Mod Constant2
We don't really need a next and previous variables, so we can say:
number = (number * Constant1) Mod Constant2
Constant2 is usually chosen as a prime. Why? (think next_number = 0)
Problem: Overflow!
Solution: More Math!
A = Constant 1; M = Constant 2; Q = M / A; R = M % A; number = ( A * (number mod Q) ) - ( R * floor(number / Q) ); if (number is negative) number = number + M; end
The idea of the above code is to avoid overflow by rearranging the equation such that no part can ever be bigger than M.