zoukankan      html  css  js  c++  java
  • C#生成不重复随机数的两个函数

    using System;   
    using System.Collections.Generic;   
    using System.Text;   
    using System.Collections;   
      
    namespace 生成N个不重复随机数的两个函数   
    {   
        class Program   
        {   
            static void Main(string[] args)   
            {   
                DateTime d1 = System.DateTime.Now;   
                int[] list1 = GetRandom1(1, 100000, 5000);   
                TimeSpan dd1 = System.DateTime.Now - d1;   
      
                DateTime d2 = System.DateTime.Now;   
                int[] list2 = GetRandom2(1, 100000*100, 5000);   
                TimeSpan dd2 = System.DateTime.Now - d2;   
      
                //foreach (int i in list1)   
                //    Console.Write("{0},", i);   
                //foreach (int ii in list2)   
                //    Console.Write("{0},", ii);   
      
                Console.WriteLine("第一种方法,1-100000    里取5000个用时:{0}", dd1.TotalMilliseconds);   
                Console.WriteLine("第二种方法,1-100000*100里取5000个用时:{0}", dd2.TotalMilliseconds);   
      
                Console.WriteLine("判断第二种方法里是否有重复数,如果是5000就是没有重复的:{0}",RemoveDup(list2).Length);   
                Console.ReadKey();   
      
            }   
      
            public static int[] RemoveDup(int[] myData)   
            {   
                if (myData.Length > 0)   
                {   
                    Array.Sort(myData);   
                    int size = 1;   
                    for (int i = 1; i < myData.Length; i++)   
                        if (myData[i] != myData[i - 1])   
                            size++;   
                    int[] myTempData = new int[size];   
                    int j = 0;   
                    myTempData[j++] = myData[0];   
                    for (int i = 1; i < myData.Length; i++)   
                        if (myData[i] != myData[i - 1])   
                            myTempData[j++] = myData[i];   
                    return myTempData;   
                }   
                return myData;   
            }     
      
      
            //方法1   
            public static int[] GetRandom1(int minValue, int maxValue, int count)   
            {   
      
                Random rnd = new Random();   
                int length = maxValue - minValue + 1;   
                byte[] keys = new byte[length];   
                rnd.NextBytes(keys);   
                int[] items = new int[length];   
                for (int i = 0; i < length; i++)   
                {   
                    items[i] = i + minValue;   
                }   
                Array.Sort(keys, items);   
                int[] result = new int[count];   
                Array.Copy(items, result, count);   
                return result;   
      
            }   
      
            //方法2   
            public static int[] GetRandom2(int minValue, int maxValue, int count)   
            {   
                int[] intList = new int[maxValue];   
                for (int i = 0; i < maxValue; i++)   
                {   
                    intList[i] = i + minValue;   
                }   
                int[] intRet = new int[count];   
                int n = maxValue;   
                Random rand = new Random();   
                for (int i = 0; i < count; i++)   
                {   
                    int index = rand.Next(0, n);   
                    intRet[i] = intList[index];   
                    intList[index] = intList[--n];   
                }   
      
                return intRet;   
            }   
      
        }   
    }   
    

  • 相关阅读:
    【总结整理】javascript的函数在if中调用时是否加括号---与.net的不同之处
    【总结整理】javascript的函数调用时是否加括号
    【总结整理】JavaScript的DOM事件学习(慕课网)
    关于overflow:hidden (转)
    CSS
    HTML
    jQuery
    函数
    装饰器
    python的条件与循环1
  • 原文地址:https://www.cnblogs.com/jayleke/p/1806835.html
Copyright © 2011-2022 走看看