生成数字+大小写英文的随机数(长度n)

第一步:写一个工具类   

import java.util.Random;

public class StdRandom {

//随机数生成器
    private static Random random;
    //种子值
    private static long seed;
    
    //静态代码块,初始化种子值及随机数生成器
    static {
        seed = System.currentTimeMillis();
        random = new Random(seed);
    }
    
    //私有构造函数,禁止实例化
    private StdRandom() {}
    
    /**
     * 设置种子值
     * @param s 随机数生成器的种子值
     */
    public static void setSeed(long s){
        seed = s;
        random = new Random(seed);
    }
    
    /**
     * 获取种子值
     * @return long 随机数生成器的种子值
     */
    public static long getSeed(){
        return seed;
    }
    
    /**
     * 随机返回0到1之间的实数 [0,1)
     * @return double 随机数
     */
    public static double uniform(){
        return random.nextDouble();
    }
    
    /**
     * 随机返回0到N-1之间的整数 [0,N)
     * @param N 上限
     * @return int 随机数
     */
    public static int uniform(int N){
        return random.nextInt(N);
    }
    
    /**
     * 随机返回0到1之间的实数 [0,1)
     * @return double 随机数
     */
    public static double random(){
        return uniform();
    }
    
    /**
     * 随机返回a到b-1之间的整数 [a,b)
     * @param a 下限
     * @param b 上限
     * @return int 随机数
     */
    public static int uniform(int a,int b){
        return a + uniform(b - a);
    }
    
    /**
     * 随机返回a到b之间的实数
     * @param a 下限
     * @param b 上限
     * @return double 随机数
     */
    public static double uniform(double a,double b){
        return a + uniform() * (b - a);
    }

}


第二步 写个测试类 就ok了


public class Test {

public static void main(String[] args) {
for(int i=1;i<=1000;i++){
// System.out.println("1-"+getRandom(32));
// System.out.println("2-o-chrv"+getRandom(24));
System.out.println("2-oGtNV"+getRandom(23));
}
}

/**
* 生成随机数
* @param len 产生的随机数长度
* @return
*/
public static String getRandom(int len) {
if(len < 3){
throw new IllegalArgumentException("字符串长度不能小于3");
}
//数组,用于存放随机字符
char[] chArr = new char[len];
//为了保证必须包含数字、大小写字母
chArr[0] = (char)('0' + StdRandom.uniform(0,10));
chArr[1] = (char)('A' + StdRandom.uniform(0,26));
chArr[2] = (char)('a' + StdRandom.uniform(0,26));
   
char[] codes = { '0','1','2','3','4','5','6','7','8','9',
'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','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'};
//charArr[3..len-1]随机生成codes中的字符
for(int i = 3; i < len; i++){
chArr[i] = codes[StdRandom.uniform(0,codes.length)];
}
       
//将数组chArr随机排序
for(int i = 0; i < len; i++){
int r = i + StdRandom.uniform(len - i);
char temp = chArr[i];
chArr[i] = chArr[r];
chArr[r] = temp;
}
     
// return new String(chArr).toUpperCase();  //数字+大写英文字母
return new String(chArr);
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值