java-获取随机字符串

import java.util.Random; 
public class getRandomString
{
    public static String excute(int length)
    { 
        StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
        StringBuffer sb = new StringBuffer();
        Random r = new Random(); 
        int range = buffer.length();
        for (int i = 0; i < length; i ++)
        { 
            sb.append(buffer.charAt(r.nextInt(range))); 
        } 
        return sb.toString(); 
    }
    public static void main(String args[])
    {
        String res = excute(4);
        System.out.println(res);
    }
}

 

posted @ 2016-03-01 20:44  243573295  阅读(1105)  评论(0编辑  收藏  举报