MyRandom - } /** Create a random lower case letter @return...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.*; public class MyRandom { static Random rand = new Random(); public static void main(String[] args) { double x; int n; char c; for (n=1; n<= 20; n++) { System.out.print(rand.nextDouble() + " "); System.out.println(rand.nextInt() + " " + randInt(-5, 10)); } } @param low the lower bound of the range @param high the upper bound of the range @return the value generated */ public static int randInt(int low, int high) { if (low > high) // bad - need to fix { int temp = low; low = high; high = temp; } int result = rand.nextInt(); result = Math.abs(result); result = result % (high - low + 1); result = result + low; return result; // send back to be value of method call } /** create a random upper case letter @return the letter generated */ public static char randUpper() { return (char) randInt('A', 'Z');
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } /** Create a random lower case letter @return the letter generated */ public static char randLower() { int code = 'a' + rand.nextInt(26); return (char) code; } /** Create a random double between low (inclusive) and high (exclusive) Pre-condition: low &lt;= high @param low the low value of the range @param high the upper bound on the range @return the value generated */ public static double randDouble(double low, double high) { return rand.nextDouble() * (high-low) + low; } /** Create a random double between low (inclusive) and high (exclusive) Pre-condition: low &lt;= high @param low the low value of the range @param high the upper bound on the range @return the value generated */ public static double randDouble(int low, int high) { return randInt(low, high-1) + rand.nextDouble(); } }...
View Full Document

This note was uploaded on 04/03/2010 for the course COMPSCI 600.107 taught by Professor Houlahan during the Spring '08 term at Johns Hopkins.

Page1 / 2

MyRandom - } /** Create a random lower case letter @return...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online