RandomNumbers

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

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

View Full Document Right Arrow Icon
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()
Background image of page 1
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 &lt; 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(&quot;You rolled &quot; + dieRoll); } /* Private instance variables */ private RandomGenerator rgen = RandomGenerator.getInstance(); }...
View Full Document

Ask a homework question - tutors are online