Returns a random boolean value. - Java java.lang

Java examples for java.lang:Math Value

Description

Returns a random boolean value.

Demo Code

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.//from   w  w  w.j a v a2 s . c  om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
import java.util.Random;

public class Main{
    static public Random random = new RandomXS128();
    /**
     * Returns a random boolean value.
     */
    static public boolean randomBoolean() {
        return random.nextBoolean();
    }
    /**
     * Returns true if a random value between 0 and 1 is less
     * than the specified value.
     */
    static public boolean randomBoolean(float chance) {
        return MathUtils.random() < chance;
    }
    /**
     * Returns a random number between 0 (inclusive) and the
     * specified value (inclusive).
     */
    static public int random(int range) {
        return random.nextInt(range + 1);
    }
    /**
     * Returns a random number between start (inclusive) and end (inclusive).
     */
    static public int random(int start, int end) {
        return start + random.nextInt(end - start + 1);
    }
    /**
     * Returns random number between 0.0 (inclusive) and 1.0
     * (exclusive).
     */
    static public float random() {
        return random.nextFloat();
    }
    /**
     * Returns a random number between 0 (inclusive) and the
     * specified value (exclusive).
     */
    static public float random(float range) {
        return random.nextFloat() * range;
    }
    /**
     * Returns a random number between start (inclusive) and
     * end (exclusive).
     */
    static public float random(float start, float end) {
        return start + random.nextFloat() * (end - start);
    }
}

Related Tutorials