zoukankan      html  css  js  c++  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 }

    注1:StdRandom类来自《算法(第4版)》 第一章 《基础编程模型》, 作者 Robert Sedgewick 、 Kevin Wayne。本文中的StdRandom类是精简版本,删除了其他未用到的方法。

  • 相关阅读:
    Java中取两位小数
    MySQL的IFNULL函数
    JS计算本周一和本周五的日期
    巅峰,不在一万米的边缘
    一段遍历4X4表格,取出每个单元格内容组合成文本的JS代码
    一个用于发送HTML格式邮件的类
    在Java程序中做字符串拼接时一定要记得的MessageFormat.format
    人就这么一辈子,要做自己想做的事情
    错误: 类Something是公共的, 应在名为 Something.java 的文件中声明
    Java中,权限修饰符的权限测试
  • 原文地址:https://www.cnblogs.com/dongliyang/p/2994554.html
Copyright © 2011-2022 走看看