Java Random Boolean getBooleanWithProbability(int probabilityOfTrue)

Here you can find the source of getBooleanWithProbability(int probabilityOfTrue)

Description

Gets a random boolean with a probability of being true.

License

Open Source License

Parameter

Parameter Description
probabilityOfTrue The probability that true should be returned.

Return

A randomly generated boolean.

Declaration

public static boolean getBooleanWithProbability(int probabilityOfTrue) 

Method Source Code

//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;
        }
    }
}

Related

  1. getBoolean()
  2. getBoolean()
  3. getBoolean()
  4. getBoolean()
  5. getBoolean(double probability)
  6. randBool(float chance)
  7. randomBoolean()
  8. randomBoolean()