Java Random Int random(int n)

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

Description

Get a random integer between 0 and n-1.

License

Open Source License

Parameter

Parameter Description
n The upper bound on the value returned.

Return

A non-negative value r that is strictly less than n and is difficult to predict.

Declaration

static int random(int n) 

Method Source Code

//package com.java2s;
/**/*from  ww  w . j  a v  a  2s.  c  om*/
 * Copyright (c) 2013-14 Samuel A. Rebelsky.  All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as published 
 * by the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Get a random integer between 0 and n-1.
     *
     * @param n
     *   The upper bound on the value returned.
     * @return
     *   A non-negative value r that is strictly less than n and
     *   is difficult to predict.
     */
    static int random(int n) {
        return (int) Math.floor(n * Math.random());
    }

    /**
     * Get a random number between 0 and n-1.
     *
     * @param n
     *   The upper bound on the value returned.
     * @return
     *   A non-negative value r that is strictly less than n and
     *   is difficult to predict.
     */
    static long random(long n) {
        return (long) Math.floor(n * Math.random());
    }
}

Related

  1. random(int min, int max)
  2. random(int min, int max)
  3. random(int min, int max)
  4. random(int min, int max)
  5. random(int min, int max)
  6. random(int n)
  7. random(int numSamples)
  8. random(int range)
  9. random(int start, int end)