Here you can find the source of randomWithRange(int min, int max)
Parameter | Description |
---|---|
min | minimum value that the random integer can be |
max | maximum value that the random integer can be |
public static int randomWithRange(int min, int max)
//package com.java2s; //License from project: LGPL public class Main { /**//w w w. java 2 s . c o m * Method for generating random integer between the 2 parameters, The order of * min and max do not matter. * * @param min minimum value that the random integer can be * @param max maximum value that the random integer can be * @return a random integer */ public static int randomWithRange(int min, int max) { int range = Math.abs(max - min) + 1; return (int) (Math.random() * range) + (min <= max ? min : max); } /** * Method for generating random doubles between the 2 parameters, The order of * min and max do not matter. * * @param min minimum value that the random double can be * @param max maximum value that the random double can be * @return a random double */ public static double randomWithRange(double min, double max) { double range = Math.abs(max - min) + 1; return (Math.random() * range) + (min <= max ? min : max); } /** * Method for generating random floats between the 2 parameters, The order of * min and max do not matter. * * @param min minimum value that the random float can be * @param max maximum value that the random float can be * @return a random float */ public static float randomWithRange(float min, float max) { float range = Math.abs(max - min) + 1; return (float) (Math.random() * range) + (min <= max ? min : max); } }