Here you can find the source of random(int n)
Parameter | Description |
---|---|
n | The upper bound on the value returned. |
static int random(int n)
//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()); } }