Java Random Int getRandomInt(int lower, int upper)

Here you can find the source of getRandomInt(int lower, int upper)

Description

This method is taken from OZONE's BenchmarkImpl class

License

Open Source License

Parameter

Parameter Description
lower a parameter
upper a parameter

Return

19-Apr-2006

Declaration

public static int getRandomInt(int lower, int upper) 

Method Source Code

//package com.java2s;
/* RandomUtil.java//from   ww  w  . j  a v  a2s  .  co m
 *
 * Copyright (C) 2009 Pieter van Zyl
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */

import java.util.Random;

public class Main {
    private static Random randomGenerator = null;

    /**
     * 
     * This method is taken from OZONE's BenchmarkImpl class
     * 
     * @param lower
     * @param upper
     * @return 19-Apr-2006
     */
    public static int getRandomInt(int lower, int upper) {
        if (randomGenerator == null) {
            randomGenerator = new Random();
            randomGenerator.setSeed(1L);
        }

        int value;
        do {
            value = randomGenerator.nextInt();
            value %= upper;
            System.out.println("value: " + value + " lower: " + lower + " upper: " + upper);
        } while (value < lower || value >= upper);
        return value;
    }

    /**
     * 
     * nextValue >= 0
     * 
     * @return 19-Apr-2006
     */
    public static int nextInt() {
        int nextValue = 0;

        nextValue = Math.abs(getRandomGenerator().nextInt());
        //nextValue = nextPositiveInt();

        return nextValue;

    }

    /**
     * 
     * added RandomUtil.getRandomGenerator() becuase Math.random() % value
     * always returned 1 as the value. ALSO: using the same random number
     * generator creates a better spread and less repetition that using a new
     * one/obj each time
     * 
     * @return 19-Apr-2006
     */
    private static Random getRandomGenerator() {
        if (randomGenerator == null) {
            randomGenerator = new Random();
            // SecureRandom
            randomGenerator.setSeed(1L);
        }
        return randomGenerator;
    }
}

Related

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