import java.util.Random;
/**
* 随机生成位数工具类
* @author xc
*/
public class RandomUtils {
// public static void main(String[] args) {
// Random random = new Random();
// System.out.println(random.nextInt(4));
//
// }
/**
* 随机数50-150
*/
public static int getRandomNum(){
Random random = new Random();
int x = random.nextInt(100);
return x+50;
}
/**
* 手机注册六位校验码生成
*/
public static String getRandomNum6(){
Random random = new Random();
int x = random.nextInt(899999);
return x+100000+"";
}
/**
* 手机注册四位校验码生成
*/
public static String getRandomNum4(){
Random random = new Random();
int x = random.nextInt(8999);
return x+1000+"";
}
/**
* 生产3位数的随机数
*/
public static String getRandomNum3(){
Random random = new Random();
int x = random.nextInt(899);
return x+100+"";
}
/**
* 生产20至50的随机数
*/
public static int getRandomNumTwentyToFifty (){
Random random = new Random();
int x = random.nextInt(30);
return x+20;
}
/**
* 生成n个包含数字和字母的字符串
*/
public static String getItemID( int n ) {
String val = "";
Random random = new Random();
for ( int i = 0; i < n; i++ )
{
String str = random.nextInt( 2 ) % 2 == 0 ? "num" : "char";
if ( "char".equalsIgnoreCase( str ) )
{ // 产生字母
int nextInt = random.nextInt( 2 ) % 2 == 0 ? 65 : 97;
// System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
val += (char) ( nextInt + random.nextInt( 26 ) );
}
else if ( "num".equalsIgnoreCase( str ) )
{ // 产生数字
val += String.valueOf( random.nextInt( 10 ) );
}
}
return val;
}
}
随机生成位数工具类 RandomUtils(包含字母的)
于 2021-12-10 09:03:06 首次发布