All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dorkbox.util.RandomUtil Maven / Gradle / Ivy

There is a newer version: 1.48
Show newest version
/*
 * Copyright 2010 dorkbox, llc
 *
 * 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.
 */
package dorkbox.util;


/**
 * This class uses the MersenneTwisterFast, which is MOSTLY random.
 */
public
class RandomUtil {

    private static final FastThreadLocal random = new FastThreadLocal() {
        @Override
        public
        MersenneTwisterFast initialValue() {
            return new MersenneTwisterFast();
        }
    };

    /**
     * Creates the thread local MersenneTwister (as it's not thread safe), if necessary
     */
    public static
    MersenneTwisterFast get() {
        return random.get();
    }

    /**
     * Returns a get integer
     */
    public static
    int int_() {
        return get().nextInt();
    }

    /**
     * Returns a number between 0 (inclusive) and the specified value (inclusive).
     */
    public static
    int int_(int range) {
        return get().nextInt(range + 1);
    }

    /**
     * Returns a number between start (inclusive) and end (inclusive).
     */
    public static
    int int_(int start, int end) {
        return start + get().nextInt(end - start + 1);
    }

    /**
     * Returns a boolean value.
     */
    public static
    boolean bool() {
        return get().nextBoolean();
    }

    /**
     * Returns number between 0.0 (inclusive) and 1.0 (exclusive).
     */
    public static
    float float_() {
        return get().nextFloat();
    }

    /**
     * Returns a number between 0 (inclusive) and the specified value (exclusive).
     */
    public static
    float float_(float range) {
        return get().nextFloat() * range;
    }

    /**
     * Returns a number between start (inclusive) and end (exclusive).
     */
    public static
    float float_(float start, float end) {
        return start + get().nextFloat() * (end - start);
    }


    /**
     * Places random bytes in the specified byte array
     */
    public static
    void bytes_(byte[] bytes) {
        get().nextBytes(bytes);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy