六位整数,一共包括90万个不同的数值,我想每次随机取一定数量(几百或几千)的数值,所有取的值不重复,取到的值保存到数据库中;取完后重新开始取,什么算法比较好?

解决方案 »

  1.   

    http://zhoufoxcn.blog.51cto.com/792419/380423
      

  2.   

    http://zhoufoxcn.blog.51cto.com/792419/380423
      

  3.   

    1、放数据库要么用自增,要么用GUID,你认为用六位整数有什么正面意义呢?
    2、所谓"取完后重新开始取",如果上一批的最后一个数是111111,而下一批的第一个随机数恰巧是111111,你又如何面对?
      

  4.   

    可行的一种算法:
    使用System.Collections.ObjectModel.Collection类做唯一性判别理论上比自己写一个排序查找算法要快一点,大概做法如下
    System.Collections.ObjectModel.Collection<int> col = new System.Collections.ObjectModel.Collection<int>();
    for(int i=0;i<qty;i++){
       //取随机数找到一个值iValue
       
        if(!col.Contains(iValue)){
            i--;
        }
    }
      

  5.   

    很遗憾,回复无响应,
    可使用System.Collections.ObjectModel.Collection类Contains方法判断是否取重复了
      

  6.   

    鉴于几百或几千相对于90万还是很小,所以可以简单处理:        public static int[] GetRandomNumber(int min, int max, int count){
                var list = new HashSet<int>();
                Random r= new Random();
                while(list.Count<count){
                    int n = r.Next(min, max);
                    list.Add(n);
                }
                return list.ToArray();
            }
      

  7.   

    鉴于几百或几千相对于90万还是很小,所以可以简单处理:        public static int[] GetRandomNumber(int min, int max, int count){
                var list = new HashSet<int>();
                Random r= new Random();
                while(list.Count<count){
                    int n = r.Next(min, max);
                    list.Add(n);
                }
                return list.ToArray();
            }
      

  8.   

    鉴于几百或几千相对于90万还是很小,所以可以简单处理:        public static int[] GetRandomNumber(int min, int max, int count){
                var list = new HashSet<int>();
                Random r= new Random();
                while(list.Count<count){
                    int n = r.Next(min, max);
                    list.Add(n);
                }
                return list.ToArray();
            }