EZ Programing Demos

Slow motion, step by step, animated demonstrations of basic computer programing techniques

Links to topics:

Current topic:

Random

The first thing to know about random numbers in most common computer languages is that they are not random. They are calculated using a preset method. However, if calculated repeatedly, the sequence of numbers produced appear to be randomly distributed, at least if you do not look too deeply. So this method works for simulating random numbers in many situations, notably games.

Computer languages often contain a function that outputs a random number. Although parentheses for input usually follow the random function name (like they do for any function), often the parentheses are left empty, since an input could have no effect on the output if the output is to be random.

 

In our demo here the name of the random function is rnd. It produces a random number between 0 and 1. Well, it almost does that; it never will really produce a 1, but it will get very close to 1. So its range would more correctly be stated as from and including 0 up to but not including 1. Or you might say from 0 to 0.99... . As it turns out, this is sufficient for producing random numbers over a wider range using additional mathematics. This is shown in the article titled 'Random over a range'.

Click the run button on this demo several times and note the group of numbers that is produced from all the outputs. All the numbers will be between 0 and 0.99... .

 Slow  Medium  Fast 
Click 'Run' to go.

A random number generator as found in most computer languages will produce the same sequence of seemingly random numbers every time the program is run. That is because the random numbers are actually calculated using a method that starts and proceeds the same way every time the program is run.

If you want to get a different sequence of random numbers, the trick is to change the starting parameters for the random number calculating method. This is called seeding the random number generator. Most computer languages contain commands that let you seed the random number generator.

 

Here would be the flowchart for this simple output:

 

In an imaginary computer language this could look like the following:

    
    print(rnd());
    

 

Suggested next article:

At EZ Math Movie

EZ Math Movie is a site that can help you experiment with and understand many topics in mathematics. Its main feature is an interactive animated (x, y) graph that you control with an actual programing language. There are many examples and tutorials, and EZ Math Movie is crossed referenced with both EZ Programing Demos and Zona Land Education.

 

Zona Land Education is a site with explanations and interactive diagrams covering many topics in physics and mathematics. Zona Land Education is cross referenced with EZ Math Movie, and it contains several animations that use EZ Math Movie's programing language.