Java生成随机字符串

Java生成随机字符串,必须包含数字、小写字母、大写字母

一道算法题,生成随机字符串,必须包含数字、小写字母、大写字母。

为了生成随机数方便,特别编写StdRandom类(注1),API如下。

public class StdRandom  
static double random() 0到1之间的实数 [0,1)
static  int uniform(int N) 0到N-1之间的整数[0,N)
static  int uniform(int lo,int hi) lo到hi-1之间的整数[lo,hi)
static  double uniform(double lo,double hi) lo到hi之间的实数[lo,hi)

 了解了StdRandom类API以后,就来看一下生成随机字符串的RandomStr类是如何完成任务的。(辅助类StdRandom最后介绍。)

RandomStr.java

复制代码
 1 public class RandomStr {
 2     
 3     /**
 4      * 单元测试
 5      * 运行: java RandomStr 4  (生成长度为4的字符串)
 6      */
 7     public static void main(String[] args){
 8         int len = Integer.parseInt(args[0]);;
 9         System.out.println(randomStr(len));
10     }
11     
12     /**
13      * 返回随机字符串,同时包含数字、大小写字母
14      * @param len 字符串长度,不能小于3
15      * @return String 随机字符串
16      */
17     public static String randomStr(int len){
18         if(len < 3){
19             throw new IllegalArgumentException("字符串长度不能小于3");
20         }
21         //数组,用于存放随机字符
22         char[] chArr = new char[len];
23         //为了保证必须包含数字、大小写字母
24         chArr[0] = (char)('0' + StdRandom.uniform(0,10));
25         chArr[1] = (char)('A' + StdRandom.uniform(0,26));
26         chArr[2] = (char)('a' + StdRandom.uniform(0,26));
27         
28     
29         char[] codes = { '0','1','2','3','4','5','6','7','8','9',
30                          'A','B','C','D','E','F','G','H','I','J',
31                          'K','L','M','N','O','P','Q','R','S','T',
32                          'U','V','W','X','Y','Z','a','b','c','d',
33                          'e','f','g','h','i','j','k','l','m','n',
34                          'o','p','q','r','s','t','u','v','w','x',
35                          'y','z'};
36         //charArr[3..len-1]随机生成codes中的字符
37         for(int i = 3; i < len; i++){
38             chArr[i] = codes[StdRandom.uniform(0,codes.length)];
39         }
40         
41         //将数组chArr随机排序
42         for(int i = 0; i < len; i++){
43             int r = i + StdRandom.uniform(len - i);
44             char temp = chArr[i];
45             chArr[i] = chArr[r];
46             chArr[r] = temp;
47         }
48         
49         return new String(chArr);
50     }
51 }
复制代码

 

看一下辅助类StdRandom。

StdRandom.java

复制代码
 1 public final class StdRandom {
 2     
 3     //随机数生成器
 4     private static Random random;
 5     //种子值
 6     private static long seed;
 7     
 8     //静态代码块,初始化种子值及随机数生成器
 9     static {
10         seed = System.currentTimeMillis();
11         random = new Random(seed);
12     }
13     
14     //私有构造函数,禁止实例化
15     private StdRandom() {}
16     
17     /**
18      * 设置种子值
19      * @param s 随机数生成器的种子值
20      */
21     public static void setSeed(long s){
22         seed = s;
23         random = new Random(seed);
24     }
25     
26     /**
27      * 获取种子值
28      * @return long 随机数生成器的种子值
29      */
30     public static long getSeed(){
31         return seed;
32     }
33     
34     /**
35      * 随机返回0到1之间的实数 [0,1)
36      * @return double 随机数
37      */
38     public static double uniform(){
39         return random.nextDouble();
40     }
41     
42     /**
43      * 随机返回0到N-1之间的整数 [0,N)
44      * @param N 上限
45      * @return int 随机数
46      */
47     public static int uniform(int N){
48         return random.nextInt(N);
49     }
50     
51     /**
52      * 随机返回0到1之间的实数 [0,1)
53      * @return double 随机数
54      */
55     public static double random(){
56         return uniform();
57     }
58     
59     /**
60      * 随机返回a到b-1之间的整数 [a,b)
61      * @param a 下限
62      * @param b 上限
63      * @return int 随机数
64      */
65     public static int uniform(int a,int b){
66         return a + uniform(b - a);
67     }
68     
69     /**
70      * 随机返回a到b之间的实数
71      * @param a 下限
72      * @param b 上限
73      * @return double 随机数
74      */
75     public static double uniform(double a,double b){
76         return a + uniform() * (b - a);
77     }
78 }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值