Random random = new Random();
String val="";
for ( int i = 0; i < 10; 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 ) );
}
}
java:生成随机数(字母加数字)
最新推荐文章于 2024-08-12 10:34:55 发布