Take the random number of the specified range
This is a very useful function, written here just to strengthen the memory, just remember a formula is OK.
such as Min, Max, Max, the random number R range to be taken is Min≤r≤max, the formula is as follows:
(int) ((max-min + 1) * Math.random ()) + min
You can see that there is nothing to say.
The implementation code is as follows:
/*
* Date Created 2004-12-11
*
*/
/**
* Take the random number of the specified range
*
* @author Wubai
*
*/
public class Getrandom {
/**
* Take a random number of the specified range
*
* @param min start range
* @param max end range
* @return Random number
*/
public int getrandom (int min, int max) {
return (int) ((max-min + 1) * Math.random ()) + min;
}
//test with
public static void Main (string[] args) {
getrandom test = new Getrandom ();
int random = Test.getrandom (10, 99);
System.out.println ("Random is" + random);
}
}