Java random numbers.


import java.util.Random;

public class Randomness {
	public static void main(String [] args) {
		// objects (other than Strings) are created using "new"
		Random rand = new Random(); // create a random number generator
		
		// Change this program so that it has an arbitrary probability
		// of rain. 
		
		System.out.println("Will it rain tonight?");
		
		int guess = rand.nextInt(2); // an int in 0, 1 range
		
		if (guess == 0) {
			System.out.println("It will not rain tonight");	
		} else {
			System.out.println("It will rain tonight!");	
		}
	}
}

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.