java生成N位随机数(字母+数字)组合

2年前 (2022) 程序员胖胖胖虎阿
177 0 0
package com.railway.common.utils;

/**
 * Created by Administrator on 2022/1/27 0027
 */

import java.security.SecureRandom;
import java.util.Random;
public class RandomUtil {
    private static final String SYMBOLS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 数字和26个字母组成
    private static final Random RANDOM = new SecureRandom();
    public static void main(String[] args) {
        System.out.println(getRandomNumber());
    }

    /**
     * 获取长度为 6 的随机字母+数字
     * @return 随机数字
     */
    public static String getRandomNumber() {
        char[] nonceChars = new char[6];  //指定长度为6位/自己可以要求设置

        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }
}

版权声明:程序员胖胖胖虎阿 发表于 2022年11月21日 下午10:48。
转载请注明:java生成N位随机数(字母+数字)组合 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...