Random Number Generation
Introduction.
It shouldn't surprise that some thing as sequential and on-offish as
computer operations are inherently incommensurate with doing something
like generating random numbers. In fact the phrase
computer generated random numbers
should be an oxymoron. But, even though they are outside the scope
of this course, there are algorithms to generate random numbers on
computers. It's an important field of study in computing. Philosophically, I find it both more and worse than amusing that people have developed algorithms to generate sequences of numbers that fool both observers and statistical tests so that they are accepted as random. It really does say something! But without getting into the actual algorithms we will make use of a method called random( ) in the java.lang.Math class to get our random numbers that are equally likely to be any number between 0 and 1. Our first program that will generate and display 15 random numbers should be called RandomNumbers,java. This is an application. |
import javax.swing.JOptionPane; public class RandomNumbers {
} |
When running this you should see something similar to the following output. Hopefully, the program uses a different seed number, such as the current time, to initialize the random( ) generated sequence of numbers so you should see somewhat different numbers. |
Notice that these numbers are all in double precision
with a lot of numbers after the decimal place. This program does the following
Random Numbers in Different Ranges. Well, now we've seen how to generate random numbers with a huge number of values after the decimal place. How can you get random numbers that follow other distributions? For example, normally distributed random numbers to represent the possible heights of the users of your new car design. Or maybe exponentially distributed random numbers to represent the times between people arriving to a bank. This is also a large area of study. We will only deal with one of the simplest possibilities where we want random numbers that are equally likely to be anything between some lower limit and some upper limit. You should have studied this distribution in your Statistics classes and called it the Uniform Distribution where numbers are distributed between a and b. Rather than go through the derivation I will just state that the solution is to do the following. |
This will be used in the following program that gets
the lower and upper limits from the user and displays 15 computer
generated random numbers that are equally likely to be between the lower
and upper limit. You should call this application RandomRange.java. |
import javax.swing.JOptionPane; public class RandomRange {
} |
After running it with a lowerLimit = 10 and an upperLimit = 25, you should see something like the following. |
The only real differences between this code and the
previous code are
This is all we will say on this topic. In the next webpage we will modify this somewhat to get random numbers that represent the roll of a single die. |