Random Numbers in java
This little program illustrates techniques for working with random numbers in Java. The program consists of a class called RandomNumbersDemo.
To create the tutorial in NetBeans, create an empty project and add a class called RandomNumbersDemo, then paste in the code below.
/**
* Random Numbers in Java
* nicomp
* Graphical Network Programming
*
* Concepts: "seed", pseudo-random, simulation
*/
package randomnumbers;
import java.util.Random; // The Random numnber generator class
/**
* Random numbers are a useful tool for simulations and also for testing.
* We don't want the numbers to be too random: the sequences must be reproducable.
* http://docs.rinet.ru/JavDev/ch34.htm
* @author Nicholson.Bill
*/
public class RandomNumbersDemo {
static final int numberCount = 1000000;
static void RunDemo() {
int i; long sum;
System.out.println("RandomNumbersDemo.Demo():");
// Each instance of the Random class is a random number generator.
// Numbers are uniformly distributed over the range of java integers.
Random myRandom = new Random(); // Don't override the seed.
System.out.println("10 random numbers");
for (i = 0; i < 10; i++) {
System.out.println(myRandom.nextInt());
}
sum = 0;
for (i = 0; i < numberCount; i++) {
sum += myRandom.nextInt();
}
System.out.println("The average of " + numberCount + " random numbers is " + sum / numberCount);
// ========================================================================
// = Declare and instantiate a random number object with a specific seed.
// = The seed provides us with repeatability.
// ========================================================================
Random myRandomWithSeed = new Random();
int j;
System.out.println("Calculating the averages from 10 random number sequences that start with the same seed.");
for (j = 0; j < 10; j++) {
sum = 0;
myRandomWithSeed = new Random(42);
// myRandomWithSeed.setSeed(42); // Reset the generator to a known state.
for (i = 0; i < numberCount; i++) {
sum += myRandomWithSeed.nextInt();
}
// This calculated average should *never* change. Why?
System.out.println("The average of " + numberCount + " random numbers is " + sum / numberCount);
}
// Floating point numbers can also be generated.
float floatingPointSum = 0, max = 0, min = 1, num;
for (i = 0; i < numberCount; i++) {
num = myRandom.nextFloat();
if (max < num) max = num;
if (min > num) min = num;
floatingPointSum += num;
}
// We can also pass an upper limit to the generate method in the random class
// to obtain a number in a specific range.
// The range for this example is between 0 and 41, inclusive.
myRandom.nextInt(42); // Upper limit is 42 - 1
// The floating point range is zero 0 to almost 1. That's very useful. Why?
System.out.println("The average of " + numberCount + " floating point random numbers is " + floatingPointSum / numberCount);
System.out.println("The max is " + max );
System.out.println("The min is " + min );
// Just out of curiousity, let's see if we ever get a 1. Does it go as high as 1?
System.out.println("Looking for a randomly generated floatig point 1.0 ...");
float counter = 0; // a float counter?
while (true) {
counter++;
if ((counter % 100000000)== 0) {
System.out.print(".");
counter = 0;
}
num = myRandom.nextFloat();
if (num == 1.0) {System.out.println(counter + " iterations required"); break;}
}
System.out.println("We found a 1.");
System.out.println("Method Finished");
}
}
