`
caoyinghui
  • 浏览: 125926 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java生成不重复的随机数

    博客分类:
  • Java
阅读更多
import java.util.*;
public class Test...{
    public static void main(String[] args)...{
        //生成 [0-n) 个不重复的随机数
        //list 用来保存这些随机数
        ArrayList list = new ArrayList();
        
        
        int n = 10;
        Random rand = new Random();
        boolean[] bool = new boolean[n];
        
        int num =0;
        
        for (int i = 0; i<n; i++)...{
            
    
            do...{
                //如果产生的数相同继续循环
                num = rand.nextInt(n);    
             
            }while(bool[num]);
            
            bool[num] =true;
            
            list.add(num);
        
        
        }
            
    
        System.out.println (list);    
        
        
    }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/caoyinghui1986/archive/2008/05/16/2450492.aspx

 

分享到:
评论
2 楼 hulushangxian 2010-07-26  
laoqian9527 写道
你这个在项目中能适用嘛?
当你的操作次数变多时,你的list中将有很多数据,这会影响效率的

怎么提高效率呀
1 楼 laoqian9527 2010-06-07  
你这个在项目中能适用嘛?
当你的操作次数变多时,你的list中将有很多数据,这会影响效率的

相关推荐

Global site tag (gtag.js) - Google Analytics