Java Random Int getRandomInt(final int maxValue)

Here you can find the source of getRandomInt(final int maxValue)

Description

Returns a random value between 0 and the maxValue minus one.

License

Apache License

Parameter

Parameter Description
maxValue The maximum value plus one for the random value.

Return

A random integer between 0 (inclusive) and the specified value minus one.

Declaration

public static int getRandomInt(final int maxValue) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .  ja  v a 2  s . c o m*/
 * Copyright 2006-2014 Andy King (GreatLogic.com)
 * 
 * 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 {
    private static Random _random;

    /**
     * Returns a random value between 0 and the maxValue minus one.
     * @param maxValue The maximum value plus one for the random value.
     * @return A random integer between 0 (inclusive) and the specified value minus one.
     */
    public static int getRandomInt(final int maxValue) {
        return _random.nextInt(maxValue);
    }

    /**
     * Returns a random value between a minimum and maximum value minus one.
     * @param minValue The minimum value for the random value.
     * @param maxValue The maximum value plus one for the random value.
     * @return A random integer between the minimum and maximum value minus one.
     */
    public static int getRandomInt(final int minValue, final int maxValue) {
        return _random.nextInt(maxValue - minValue) + minValue;
    }
}

Related

  1. getRandomInt()
  2. getRandomInt()
  3. getRandomInt()
  4. getRandomInt(final int limit)
  5. getRandomInt(final int max)
  6. getRandomInt(final int min, final int max, final Random random)
  7. getRandomInt(int bound)
  8. getRandomInt(int intStart, int intEnd)
  9. getRandomInt(int lower, int upper)