Java Random Int getRandomInteger(int min, int max)

Here you can find the source of getRandomInteger(int min, int max)

Description

Get a random value from an interval.

License

Open Source License

Parameter

Parameter Description
min The minimum value (>= 0).
max The maximum value (>= 0 && >= min).

Return

A value between min inclusive and max inclusive.

Declaration

public static int getRandomInteger(int min, int max) 

Method Source Code


//package com.java2s;
/*/* w  w  w.  j av a2  s. com*/
 * Copyright (C) 2013-2014 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre (contact@b3dgs.com)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

import java.util.Random;

public class Main {
    /** The random utility instance. */
    private static final Random RANDOM = new Random();

    /**
     * Get a random integer value from 0 and 2^32.
     * 
     * @return A value between 0 and 2^32.
     */
    public static int getRandomInteger() {
        return RANDOM.nextInt();
    }

    /**
     * Get a random value from 0 and a maximum.
     * 
     * @param max The maximum randomized value.
     * @return A value between 0 inclusive and max inclusive.
     */
    public static int getRandomInteger(int max) {
        return RANDOM.nextInt(max + 1);
    }

    /**
     * Get a random value from an interval.
     * 
     * @param min The minimum value (>= 0).
     * @param max The maximum value (>= 0 && >= min).
     * @return A value between min inclusive and max inclusive.
     */
    public static int getRandomInteger(int min, int max) {
        return min + RANDOM.nextInt(max + 1 - min);
    }
}

Related

  1. getRandomInt(Random rand, int lower, int upper)
  2. getRandomInt(String level)
  3. getRandomIntBetween(int low, int high)
  4. getRandomIntBetween(int mi, int ma)
  5. getRandomInteger(int aStart, int aEnd)
  6. getRandomIntegerArray(Random rand, int size, int range)
  7. getRandomIntegerInRange(Random p_76136_0_, int p_76136_1_, int p_76136_2_)
  8. getRandomIntegerInRange(Random random, int i, int j)
  9. getRandomInterests()