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

craterdog.utils.RandomUtils Maven / Gradle / Ivy

There is a newer version: 3.12
Show newest version
/************************************************************************
 * Copyright (c) Crater Dog Technologies(TM).  All Rights Reserved.     *
 ************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.        *
 *                                                                      *
 * This code is free software; you can redistribute it and/or modify it *
 * under the terms of The MIT License (MIT), as published by the Open   *
 * Source Initiative. (See http://opensource.org/licenses/MIT)          *
 ************************************************************************/
package craterdog.utils;

import java.security.SecureRandom;


/**
 * This utility class contains static methods that generate random values for various purposes.
 *
 * @author Derk Norton
 */
public class RandomUtils {

    static public final SecureRandom generator = new SecureRandom();


    /**
     * This utility method returns a randomly generated integer value.
     *
     * @return The randomly selected integer value.
     */
    static public int pickRandomInt() {
        return generator.nextInt();
    }


    /**
     * This utility method randomly selects an index from a range of indexes.  It is useful for
     * randomly selecting an element of a list or array.  For example:
     * 
     *    int randomIndex = RandomUtils.pickRandomIndex(list.size());
     * 
     * Note, the maximum possible index will be one less than the range (0..range-1).
     *
     * @param range The size of the index range (including 0).
     * @return The randomly selected index value.
     */
    static public int pickRandomIndex(int range) {
        return generator.nextInt(range);
    }


    /**
     * This utility method returns an evenly distributed randomly generated double value.
     *
     * @return The randomly selected double value.
     */
    static public double pickRandomProbability() {
        return generator.nextDouble();
    }


    /**
     * This utility method returns a Gaussian distributed randomly generated double value.
     *
     * @return The randomly selected double value.
     */
    static public double pickRandomGaussian() {
        return generator.nextGaussian();
    }


    /**
     * This utility method returns a byte array of the specified size containing randomly generated bytes.
     *
     * @param numberOfBytes The number of bytes in the desired byte array.
     * @return The byte array containing random bytes.
     */
    static public byte[] generateRandomBytes(int numberOfBytes) {
        byte[] result = new byte[numberOfBytes];
        generator.nextBytes(result);
        return result;
    }


    private RandomUtils() {
        // should never be called
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy