码迷,mamicode.com
首页 > 编程语言 > 详细

java随机生成指定的位数

时间:2016-07-07 19:24:22      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

    /**
     * 获取count个随机数
     * @param count 随机数个数
     * @return
     */
    public static String game(int count){
        StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for(int i=0;i<count;i++){
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num)+""), "");
        }
        return sb.toString();
    }
 
    public static void main(String[] args) {

        System.out.println(game(4));
    }

 

java随机生成指定的位数

标签:

原文地址:http://www.cnblogs.com/sunhaoyu/p/5651030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!