Java examples for Algorithm:Random
Toss a coin
import java.util.Random; public class CoinToss { public String tossACoin() { Random rand = new Random(); int toss = Math.abs(rand.nextInt()) % 2; if (toss == 0) { return "HEADS"; } else {/*www .ja va2 s . co m*/ return "TAILS"; } } public static void main(String[] args) { // TODO code application logic here CoinToss game = new CoinToss(); System.out.println(game.tossACoin()); System.out.println(game.tossACoin()); System.out.println(game.tossACoin()); System.out.println(game.tossACoin()); System.out.println(game.tossACoin()); System.out.println(game.tossACoin()); System.out.println(game.tossACoin()); System.out.println(game.tossACoin()); } }