*/
public static int getRandomInt( int min, int max )
{
// include min, exclude max
int result = min + new Double( Math.random() * ( max - min ) ).intValue();
return result;
}
/**
* get a random double with the range [min, max)
* @param min the minimum value
* @param max the maximum value
* @return the random double value
*/
public static double getRandomDouble( double min, double max )
{
// include min, exclude max
double result = min + ( Math.random() * ( max - min ) );
return result;
}
/**
*
* @return a random char with the range ASCII 33(!) to ASCII 126(~)
*/
public static char getRandomChar()
{
// from ASCII code 33 to ASCII code 126
int firstChar = 33; // "!"
int lastChar = 126; // "~"
char result = ( char ) ( getRandomInt( firstChar, lastChar + 1 ) );
return result;
}
/**
*
* @return a random rgb color
*/
public static RGB getRandomRGB()
{
int red = getRandomInt(0,256);
int green = getRandomInt(0,256);
int blue = getRandomInt(0,256);
return new RGB( red, green, blue );
}
/**
*
* @return a random char with the range [0-9],[a-z],[A-Z]
*/
public static char getRandomNormalChar()
{
// include 0-9,a-z,A-Z
int number = getRandomInt( 0, 62 );
int zeroChar = 48;
int nineChar = 57;
int aChar = 97;
int zChar = 122;
int AChar = 65;
int ZChar = 90;
char result;
if( number < 10 )
{
result = ( char ) ( getRandomInt( zeroChar, nineChar + 1 ) );
return result;
}
else if( number >= 10 && number < 36 )
{
result = ( char ) ( getRandomInt( AChar, ZChar + 1 ) );
return result;
}
else if( number >= 36 && number < 62 )
{
result = ( char ) ( getRandomInt( aChar, zChar + 1 ) );
return result;
}
else
{
return 0;
}
}
/**
*
* @param length the length of the String
* @return a String filled with random char
*/
public static String getRandomString( int length )
{
// include ASCII code from 33 to 126
StringBuffer result = new StringBuffer();
for( int i = 0; i < length; i++ )
{
result.append( getRandomChar() );
}
return result.toString();
}
/**
*
* @param length the length of the String
* @return a String filled with normal random char
*/
public static String getRandomNormalString( int length )
{
// include 0-9,a-z,A-Z
StringBuffer result = new StringBuffer();
for( int i = 0; i < length; i++)
{
result.append( getRandomNormalChar() );
}
return result.toString();
}
}
相關(guān)推薦:計(jì)算機(jī)等級(jí)考試二級(jí)Java編程的三十個(gè)基本規(guī)則北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |