随机数字和知字母

/**
     * 生成随机数字加字母
     * n : 需要的长度
     * @return
     */
    public  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;
    }

     /**
     * 生成随机数字
     * n : 需要的长度
     * @return
     */
    public  String getStr( int n ){
        String val = "";
        Random random = new Random();
        for ( int i = 0; i < n; i++ ){
            val += String.valueOf( random.nextInt( 10 ) );
        }
        return val;
    }

猜你喜欢

转载自blog.csdn.net/zengshangchun/article/details/84956491