12345678910111213141516171819202122232425 |
- package com.huimv.wine.utils;
- import java.util.Random;
-
- public class Utils {
- private static final String ALPHA_NUMERIC_STRING = "QWERTYUIOPLKJHGFDSAZXCVBNM0123456789zxcvbnmlkjhgfdsaqwertyuiop";
- private static final int ALPHA_NUMERIC_STRING_LENGTH = ALPHA_NUMERIC_STRING.length();
- private static final Random RANDOM = new Random();
-
- /**
- * 生成指定长度的随机字符串,只包含字母和数字。
- *
- * @param length 字符串长度
- * @return 随机生成的字符串
- */
- public static String randomString(int length) {
- StringBuilder builder = new StringBuilder();
- while (length-- > 0) {
- int index = (int) (RANDOM.nextFloat() * ALPHA_NUMERIC_STRING_LENGTH);
- char randomChar = ALPHA_NUMERIC_STRING.charAt(index);
- builder.append(randomChar);
- }
- return builder.toString();
- }
- }
|