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 { public static void main( String args[ ] ) { int i; double value; String output = "15 Computer Generated\nRandom Numbers\n\nbetween 0 and 1\n\n"; for (i=1; i<=15; i++) { value = Math.random( ); output = output + "\t" + value + "\n"; } JOptionPane.showMessageDialog( null, output, "Random Numbers", JOptionPane.INFORMATION_MESSAGE ); System.exit(0); } }

 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 the first section of code imports the swing.JOptionPane class declares the overall inclusive class RandomNumbers within the method main( ) declares a counter declares a double used for a place to return the random numbers initializes a string used for output develops a for loop block of code to generate the random numbers using Math.random( ) adds these to the output string each on their own line displays the output using the showMessageDialog( ) method from the JOptionPane class in the swing package then it ends the method main( ) 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 { public static void main( String args[ ] ) { int i; double value; double lowerLimit, upperLimit; String strLowerLimit, strUpperLimit; String output = "15 Computer Generated\nRandom Numbers\n\n"; // obtaining the lower and upper limits for the distribution strLowerLimit = JOptionPane.showInputDialog("Please enter the lower limit of the distribution:"); strUpperLimit = JOptionPane.showInputDialog("Please enter the upper limit of the distribution:"); // parsing the inputs lowerLimit = Double.parseDouble(strLowerLimit); upperLimit = Double.parseDouble(strUpperLimit); output = output + "Between " + lowerLimit + " and " + upperLimit + "\n\n"; for (i=1; i<=15; i++) { value = lowerLimit + ((upperLimit - lowerLimit) * Math.random()); output = output + "\t" + value + "\n"; } JOptionPane.showMessageDialog( null, output, "Random Numbers", JOptionPane.INFORMATION_MESSAGE ); System.exit(0); } }

 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 more variables for inputs and outputs are declared the lower limit and upper limit must be obtained from the user and parsed into doubles I skipped the error trapping due to the fact that this is just a quick demo the output reflects the lower and upper limits entered by the user the  function to compute the random number value is slightly more complicated as a function of the lowerLimit and upperLimit 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.