RandomNumbers

# RandomNumbers - Returns a random double d in the range 0...

This preview shows page 1. Sign up to view the full content.

1 And now a word from our friend… the Random Number Generator RandomGenerator import acm.program.*; import acm.util.*; public class SimpleRandom extends ConsoleProgram { public void run() { // Will fill in shortly } /* Private instance variables */ private RandomGenerator rgen = RandomGenerator.getInstance(); } Methods to Generate Random Values The RandomGenerator class defines the following methods: int nextInt(int low, int high) Returns a random int between low and high , inclusive. int nextInt(int n) Returns a random int between 0 and n - 1. double nextDouble(double low, double high) Returns a random double d in the range low d < high . double nextDouble()
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Returns a random double d in the range 0 ≤ d < 1. boolean nextBoolean() Returns a random boolean value, which is true 50 percent of the time. boolean nextBoolean(double p) Returns a random boolean , which is true with probability p , where 0 ≤ p ≤ 1. Color nextColor() Returns a random color. Simple Random Example import acm.program.*; import acm.util.*; public class SimpleRandom extends ConsoleProgram { public void run() { int dieRoll = rgen.nextInt(1, 6); println("You rolled " + dieRoll); } /* Private instance variables */ private RandomGenerator rgen = RandomGenerator.getInstance(); }...
View Full Document

• Fall '08
• SAHAMI,M
• Randomness, Cumulative distribution function, Hardware random number generator, private instance variables, random int

{[ snackBarMessage ]}

Ask a homework question - tutors are online