Java Random Int randInt(int n)

Here you can find the source of randInt(int n)

Description

Generates a random integer in the range [0,n).

License

Open Source License

Parameter

Parameter Description
n - maximum value (exclusive)

Return

- random integer

Declaration

public static int randInt(int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from ww  w . j  av  a2 s. c o  m*/
     * Generates a random integer in the range [0,n).
     * 
     * @param n - maximum value (exclusive)
     * @return - random integer
     */
    public static int randInt(int n) {
        return (int) randInt(0, n);
    }

    /**
     * Generates a random integer in the given range.
     * 
     * @param minrange - minumum value (inclusive)
     * @param maxrange - maximum value (exclusive)
     * @return - random integer
     */
    public static int randInt(int minrange, int maxrange) {
        return (int) randRealUniform(minrange, maxrange);
    }

    /**
     * Generates a random real number in the given range. 
     * 
     * @param minrange - minimum value
     * @param maxrange - maximum value
     * @return - random number
     */
    public static double randRealUniform(double minrange, double maxrange) {
        return minrange + (Math.random() * (maxrange - minrange));
    }
}

Related

  1. randInt(int min, int max)
  2. randInt(int min, int max)
  3. randInt(int min, int max)
  4. randInt(int min, int max)
  5. randInt(int n)
  6. randInt(int n)
  7. randInt(int range)
  8. randInt(int range)
  9. RandInt(int x, int y)