/* * A class to illustrate random number generation for coin flipping. */ import java.util.Random; // Import the Random utility library function. public class RandomCoinFlip { /* * Print out a random sequence of the words "heads" and "tails" for * a random number of times. */ public static void main(String[] args) { /* Here we create a Random object. */ Random flipper = new Random(); /* * This loop randomly chooses "heads" or "tails" a random number of times. */ boolean keepGoing = true; while(keepGoing) { int flip = flipper.nextInt(2); // Choose a random number from 0 to 1. switch(flip) // Output a heads or tails based on the random number. { case 0: System.out.println("heads"); break; case 1: System.out.println("tails"); break; } flip = flipper.nextInt(10); // Choose a random number from 0 to 9. switch(flip) // Set the keepGoing flag based on the random number. { case 0: keepGoing = false; break; default: keepGoing = true; break; } } } }