1. import java.util.Random;
  2. import java.util.UUID;
  3. /**
  4. * 生产指定长度随机字符串a-z,A-Z,0-9
  5. * @author happyqing
  6. * @since 2016.5.30
  7. */
  8. public class RandomString {
  9. /**
  10. * 获取随机字符串 a-z,A-Z,0-9
  11. * @param length 长度
  12. * @return
  13. */
  14. public static String getRandomString(int length) {
  15. String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  16. Random random = new Random();
  17. StringBuffer sb = new StringBuffer();
  18. for (int i = 0; i < length; ++i) {
  19. //int number = random.nextInt(62);// [0,62)
  20. sb.append(str.charAt(random.nextInt(62)));
  21. }
  22. return sb.toString();
  23. }
  24. /**
  25. * JAVA获得0-9,a-z,A-Z范围的随机数
  26. *
  27. * @param length
  28. * 随机数长度
  29. * @return String
  30. */
  31. public static String getRandomChar(int length) {
  32. char[] chr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  33. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  34. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
  35. Random random = new Random();
  36. StringBuffer buffer = new StringBuffer();
  37. for (int i = 0; i < length; i++) {
  38. buffer.append(chr[random.nextInt(62)]);
  39. }
  40. return buffer.toString();
  41. }
  42. /**
  43. * 获取随机字符串 a-z
  44. * @param length 长度
  45. * @return
  46. */
  47. public static String getLowerLetter(int length) {
  48. String str = "abcdefghijklmnopqrstuvwxyz";
  49. Random random = new Random();
  50. StringBuffer sb = new StringBuffer();
  51. for (int i = 0; i < length; ++i) {
  52. sb.append(str.charAt(random.nextInt(26)));
  53. }
  54. return sb.toString();
  55. }
  56. /**
  57. * 获取随机字符串 0-9
  58. * @param length 长度
  59. * @return
  60. */
  61. public static String getNumber(int length) {
  62. String str = "0123456789";
  63. Random random = new Random();
  64. StringBuffer sb = new StringBuffer();
  65. for (int i = 0; i < length; ++i) {
  66. sb.append(str.charAt(random.nextInt(10)));
  67. }
  68. return sb.toString();
  69. }
  70. /**
  71. * 获取随机字符串 0-9,a-z,0-9
  72. * 有两遍0-9,增加数字概率
  73. * @param length 长度
  74. * @return
  75. */
  76. public static String getLowerLetterNumber(int length) {
  77. String str = "0123456789abcdefghijklmnopqrstuvwxyz0123456789";
  78. Random random = new Random();
  79. StringBuffer sb = new StringBuffer();
  80. for (int i = 0; i < length; ++i) {
  81. sb.append(str.charAt(random.nextInt(46)));
  82. }
  83. return sb.toString();
  84. }
  85. /**
  86. * 获取随机密码,lLength位小写英文+nLength位数字
  87. * @param lLength 字母长度
  88. * @param nLength 数字长度
  89. * @return
  90. */
  91. public static String getPasswordSimple(int lLength, int nLength) {
  92. return getLowerLetter(lLength)+getNumber(nLength);
  93. }
  94. /**
  95. * 获取随机密码,包含英文和数字
  96. * @param length 密码长度
  97. * @return
  98. */
  99. public static String getPasswordComplex(int length) {
  100. String password;
  101. while(true){
  102. password = getLowerLetterNumber(6);
  103. //必须包含字母和数字
  104. if (password.matches("(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}")) {
  105. return password;
  106. }
  107. }
  108. }
  109. public static void main(String[] args) {
  110. // System.out.println(getRandomString(32));
  111. // System.out.println(getRandomChar(32));
  112. // //UUID
  113. // String uuid = UUID.randomUUID().toString().replaceAll("-", "");
  114. // System.out.println(uuid);
  115. System.out.println(getPasswordSimple(1,5));
  116. System.out.println(getPasswordComplex(6));
  117. }
  118. }
  119. 转自:https://www.dandelioncloud.cn/article/details/1513449489842126850