4. JAVA

[java]랜덤a~z,0~9 조합 하여 랜덤으로 String만들기

자르르 2013. 7. 3. 10:20


public String getRandomPassword( int length ){

        char[] charaters = {'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','0','1','2','3','4','5','6','7','8','9'};

        StringBuffer sb = new StringBuffer();

        Random rn = new Random();

        for( int i = 0 ; i < length ; i++ ){

            sb.append( charaters[ rn.nextInt( charaters.length ) ] );

        }

        return sb.toString();

    }



getRandomPassword(15) 호출하면 15개의 랜덤 String이 반환 된다.