zoukankan      html  css  js  c++  java
  • java 生成随机数字+字母组合 和字母组合

    生成随机数包含数字,字母

    /**
         * 生成随机数当作getItemID
         * n : 需要的长度
         * @return
         */
        private static String getItemID( int n )
        {
            String val = "";
            Random random = new Random();
            for ( int i = 0; i < n; i++ )
            {
                String str = random.nextInt( 2 ) % 2 == 0 ? "num" : "char";
                if ( "char".equalsIgnoreCase( str ) )
                { // 产生字母
                    int nextInt = random.nextInt( 2 ) % 2 == 0 ? 65 : 97;
                    // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
                    val += (char) ( nextInt + random.nextInt( 26 ) );
                }
                else if ( "num".equalsIgnoreCase( str ) )
                { // 产生数字
                    val += String.valueOf( random.nextInt( 10 ) );
                }
            }
            return val;
        }

    测试结果:

    UtSZ4cta

    生成字母

     /**
         * 生产ItemName随机函数
         * @param length
         * @return
         */
        private static String getItemName( int length ){
            String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            Random random = new Random();
            StringBuffer sb = new StringBuffer();
            for ( int i = 0; i < length; i++ )
            {
                int number = random.nextInt( base.length() );
                sb.append( base.charAt( number ) );
            }
            return sb.toString();
        }

    测试结果:

    cgZbhcIu

  • 相关阅读:
    Golang 归并排序(MergeSort)
    Kubernetes-PV/PVC
    Python 快速排序(QuickSort)
    Kubernetes-Service
    Docker 架构
    Deployment 工作流程
    http响应code-405
    python实现计数累增的方法
    mysql使用记录、持续更新
    mac开发环境-brew、xcode
  • 原文地址:https://www.cnblogs.com/zhanggl/p/5175879.html
Copyright © 2011-2022 走看看