PseudoRandom Number Generation in Java

by bryan on January 20, 2010

Assignment number 1 in CS211 (Java) programming class. (No stealing! I want full point on my grade!)

PseudoRandomDemo.java

 /*
|	Bryan Simonson
 \	CS211 - Craig Niiyama
  | 1/19/2010
*/

package bryansimonson;
import bryansimonson.PseudoRandom;

/**
 * @author Bryan Simonson (manticor@gmail.com)
 */
public class PseudoRandomDemo {

	/**
	 * The main function generates a table that counts how many
	 * instances of a pseudorandom number fall within a certain range.
	 */
	public static void main(String[] args) {
		final int INTRVL = 10;				// interval of distribution table
		final int LOOPS = 1000000;			// number of times to run loop
		final int SEED = 1;					// initial seed value
		final int INCR = 3641;				// increment
		final int MOD = 729;				// modulus
		final int MULT = 40;				// multiplier
		int[] dist = new int[INTRVL];		// distribution table

		PseudoRandom x = new PseudoRandom(SEED, MULT, INCR, MOD);

		System.out.println("Output of PseudoRandomDemo");
		System.out.println();

		// Show initial values
		System.out.println("Initial values");
		System.out.println("Interval:\t"+INTRVL);
		System.out.println("Seed:\t\t"+SEED);
		System.out.println("Multiplier:\t"+MULT);
		System.out.println("Increment:\t"+INCR);
		System.out.println("Modulus:\t"+MOD);
		System.out.println();

		// Demonstrate current() function
		System.out.println("Demonstrate current() function");
		System.out.println(x.current());
		System.out.println();

		// Demonstrate next() function
		System.out.println("Demonstrate next() function");
		System.out.println(x.next());
		System.out.println(x.next());
		System.out.println(x.next());
		System.out.println();

		// Demonstrate nextMod() function
		System.out.println("Demonstrate nextMod() function");
		System.out.print("nextMod() value: ");
		System.out.printf("%6.2f",x.nextMod());
		System.out.println();
		System.out.println("new seed value:"+x.current());

		// Demonstrate changeSeed() function
		x.changeSeed(SEED);
		System.out.println("\nSeed has been reset using changeSeed("+SEED+")\n");

		// Populate array with count of instances between each interval
		for (int i = 0; i<LOOPS; i++){
			dist[(int)(x.nextMod()*INTRVL)]++;
		}

		// Print out a table with instance counts per interval
		System.out.println("Count of instances");
		System.out.println("Range\t\tNumber of Occurrences");
		for (int i=0; i<INTRVL; i++){
			System.out.println(
					"["+
					((double)(i)/INTRVL)+
					".."+
					((double)(i+1)/INTRVL)+
					")\t"+
					dist[i]
			);
		}
	}
}

PseudoRandom.java (Class)

package bryansimonson;

 /*
|	Bryan Simonson
 \	CS211 - Craig Niiyama
  | 1/19/2010
*/

/**
 * @author Bryan Simonson (manticor@gmail.com)
 *
 */
public class PseudoRandom {
	private int seed;
	private int multiplier;
	private int increment;
	private int modulus;

	/**
	 * Constructor for PseudoRandom object.
	 * @param firstseed - The original seed value
	 * @param mult - Multiplier
	 * @param incr - Increment
	 * @param mod - Modulus value
	 */
	public PseudoRandom (int firstseed, int mult, int incr, int mod){
		multiplier = mult;
		increment = incr;
		modulus = mod;
		seed = (mult * firstseed + incr) % mod;
	}

	/**
	 * Access the current seed value
	 * @return
	 * 	The current seed value
	 */
	public int current(){
		return seed;
	}

	/**
	 * Generates the next pseudorandom number iteration
	 * @return
	 * 	The next pseudorandom number iteration
	 * @postcondition
	 * 	The current seed value set to the same value that is returned
	 */
	public int next(){
		seed = (multiplier * seed + increment) % modulus;
		return seed;
	}

	/**
	 * Change the current seed value
	 * @param newseed
	 * @postcondition
	 * 	The current seed value is replaced by <code>newseed</code>
	 */
	public void changeSeed(int newseed){
		seed = newseed;
	}

	/**
	 * Generates the next pseudorandom number divided by the modulus value
	 * @return
	 * 	a double in the range [0..1)
	 * @postcondition
	 * 	the current seed value is set to whatever the next pseudorandom integer
	 */
	public double nextMod(){
		return (next()/(double)modulus);
	}
}

Leave a Comment

Connect with Facebook

Previous post:

Next post: