Here you can find the source of getBooleanWithProbability(int probabilityOfTrue)
Parameter | Description |
---|---|
probabilityOfTrue | The probability that true should be returned. |
public static boolean getBooleanWithProbability(int probabilityOfTrue)
//package com.java2s; /******************************************************************************* * LogicHelper.java/* w ww . j av a 2 s.co m*/ * Copyright (c) 2014 Radix-Shock Entertainment. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html ******************************************************************************/ import java.util.Random; public class Main { /** * Gets a random boolean with a probability of being true. * * @param probabilityOfTrue * The probability that true should be returned. * * @return A randomly generated boolean. */ public static boolean getBooleanWithProbability(int probabilityOfTrue) { if (probabilityOfTrue <= 0) { return false; } else { return new Random().nextInt(100) + 1 <= probabilityOfTrue; } } }