public class Test {
? ? public static void main(String[]?args)?{
? ? ? ? intauthCode?=?makeAuthCode();
? ? ? ? System.out.println(authCode);
? ? ? ? System.out.println("-----------------------------------");
? ? }
? ? public static int makeAuthCode()?{
? ? ? ? int authCodeNew?=0;
? ? ? ? authCodeNew?=?(int)?Math.round(Math.random()?*?(9999-1000)?+1000);
? ? ? ? return authCodeNew;
? ? }
}
比較簡單,有興趣的可以嘗試
下面是commons-lang3下的一個工具類,4表示幾位的驗證碼,0-9表示驗證碼從這些數字生成
public?static?String?generateVerificationCode()?{
? ? return?RandomStringUtils.random(4,?"0123456789");
}
這個大家可以了解一下,下面是我貼的RandomStringUtils.random()的源代碼