The Need for Random Numbers
Randomness can be quite useful for a computer program. If you were playing a game, for example, you probably wouldn't want it to be exactly the same every time, so you could use random numbers for things like starting position, speed and angle. You might want to simulate a die roll or a coin toss, or you might want to quickly generate lots of data to test your program.
Computers aren't very good at generating random numbers, because they can only do things if you tell them exactly how to do it with a program or algorithm. They do their best by using things that change, such as the system time, to help them make the numbers different every time, but they're never truly random. That's why you often see random numbers generated by computers described as pseudorandom (pseudo means false or mock).
Generating Random Numbers
Multiplying and Rounding
It's unlikely, though, that you want a random number in the range 0 -
0.99999..., so you'll most-likely need to multiply the random number to make it larger. The number you need to multiply by is the number of possible outcomes that
you want - e.g. if you want to simulate the roll of a die, there are six possible outcomes, so you need to multiply by six.
Multiplying a decimal by a whole number will, unless you're very lucky, still
give you a decimal, though, so the next stage is to round the number to make it
an integer. "Integer" is the mathematical name for a whole
While it's more usual to round numbers to the nearest integer - e.g. we round
3.6 up to 4 - that won't give us an even spread of random numbers. Why
not? Well, imagine you had random decimals numbers from 0 - 10. If
you round as you would in your Maths exam, the numbers up (but not including)
0.5 would get rounded to 0, and the numbers from 0.5 up to (but not including)
1.5 would get rounded to 1. This means that twice as many numbers get
rounded to 1 as get rounded to 0 - that wouldn't be right. The same thing
would happen at the other end of the scale - only numbers that are 9.5 and above
would get rounded up to 10.
For that reason, we always round down, and most programming
languages (and spreadsheets and Access) have an int()
function that lets you do that.
Adding the Starting Value
Taking a random number in the range 0 - 0.99999..., multiplying by six and
rounding down gives us numbers in the range 0 - 5, though, rather than the 1 - 6
that a die would give us.
The final stage in the process is to add on the smallest value you would want
to get - in the case of the die, we add 1.
This technique works for any range, even if it's negative. If you
wanted numbers in the range -5 to 5, we would generate a random number and then:
multiply by 11 (as there are 11 integers from -5 to 5 if you
round down (which would give numbers in the range 0 - 10)
add -5 (i.e. subtract 5)
To do this in Excel or Open/Libre Office, you can use the
function =int(rand()*11)-5. To do it in BASIC, it would be
Math.floor(Math.random()*11) -5. For versions in other languages,
check with your teacher.
Random Things That Aren't Numbers
All the random examples so far are numbers - what about if you want to pick something else at random, such as a day of the week, or a name?
A common method is to put the choices into an array, and then use a random number as the index. If you're using a spreadsheet, you could create a random number and then use vlookup() to select the value from a table, or in Access you could use the random numbers to match records in a database table.
var day_name = new Array(7);
day_name = "Sunday";
day_name = "Monday";
day_name = "Tuesday";
day_name = "Wednesday";
day_name = "Thursday";
day_name = "Friday";
day_name = "Saturday";
document.getElementById("weekday").innerHTML = day_name[Math.floor(Math.random()*7)];
The array is created when the page is loaded, and then when the button is clicked, the Math.floor(Math.random()*7) part uses the method detailed above to generate a number from 0 - 6, which is then used as the array index to look up the day name.
The method described above creates pseudorandom numbers that are uniformly distributed throughout the chosen range. You might not always want that to be the case. On my angles page, for example, I wanted smaller angles to be chosen more often than larger ones, because most angles that students encounter are less than 180º.
The solution is surprisingly simple; because the random function in most languages generates a number between 0 and 1, if you raise that number to any power it stays between 0 and 1, but the distribution changes. If you square the number, for example, 0 stays as 0 and 1 remains 1, but 0.5 becomes 0.25 – i.e. the distribution is skewed towards the lower numbers. You need to do that before scaling. If you want to skew the distribution upwards, simply subtract the result from 1 before scaling.
If you would like to read more about scaling and distribution, read my TES article The Joy of Scaling.