ValidateCode.java 763 B

12345678910111213141516171819202122232425
  1. package com.template.common.utils;
  2. import java.util.Random;
  3. //验证码随机数
  4. public class ValidateCode {
  5. public static Integer generateValidateCode(int length){
  6. Integer code =null;
  7. if(length == 4){
  8. code = new Random().nextInt(9999);//生成随机数,最大为9999
  9. if(code < 1000){
  10. code = code + 1000;//保证随机数为4位数字
  11. }
  12. }else if(length == 6){
  13. code = new Random().nextInt(999999);//生成随机数,最大为999999
  14. if(code < 100000){
  15. code = code + 100000;//保证随机数为6位数字
  16. }
  17. }else{
  18. throw new RuntimeException("只能生成4位或6位数字验证码");
  19. }
  20. return code;
  21. }
  22. }