怎样每次生成的随机数都和之前出现过的不一样啊
(一次一次生成)

解决方案 »

  1.   

    把之前的存起来
    List<Int32> list = new List<Int32>();
    while(条件)
    {
    Random r = new Random();
    int n = r.next
    if(!list.Contains(n))
    {
    list.Add(n);
    }}
      

  2.   

    static Random r = new Random();
      

  3.   

    生成之后循环判断一次,如果是之前生成过的就再次生成.
    using System;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.Collections.Generic;
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                int Count = 0;
                List<int> r = new List<int>();
                List<int> Result = new List<int>();
                int MaxCount = 50;
                while (true)
                {
                    int i = new Random().Next(1, 1000);
                    if (!r.Contains(i))
                    {
                        r.Add(i);
                        Count++;
                    }
                    if (Count >= MaxCount) break;
                }
                Result.AddRange(r.OrderBy(i => i));
                Console.ReadKey();
            }
        }
    }
      

  4.   

    public static string RndNum1(int VcodeNum)
            {
                string Vchar = "0,1,2,3,4,5,6,7,8,9";            string[] VcArray = Vchar.Split(',');
                string VNum = "";//由于字符串很短,就不用StringBuilder了
                //int temp = -1;//记录上次随机数值,尽量避免生产几个一样的随机数            //采用一个简单的算法以保证生成随机数的不同
                Random rand = new Random();
                for (int i = 1; i < VcodeNum + 1; i++)
                {
                    rand = new Random(i * unchecked((int)DateTime.Now.Ticks));
                    int t = rand.Next(9);
                    VNum += VcArray[t];
                }
                return VNum;
            }
    里面有个安日期来验证随即数的操作
    rand = new Random(i * unchecked((int)DateTime.Now.Ticks));
      

  5.   

    利用HashSet的特性
    生成数往里加 要生成多少就加到 hashset 的 size 到多少
      

  6.   

    Random r = new Random(unchecked((int)DateTime.Now.Ticks)); 
      

  7.   

                List<Int32> list = new List<Int32>();
                System.Random R = new Random();
                Int32 temp;            //设生成36个随机数。
                for (int i = 0; i < 36; i++)
    {
                //Again是标签。
                Again:
                    temp = R.Next(1, 71);
                    if (list.Contains(temp))
                    {
                        goto Again;
                    }
                    list.Add(temp);
    }            for (int i = 0; i < list.Count ; i++)
                {
                    Console.Write(list[i] + "  ");
                }
      

  8.   

    先随机。。然后存在list中下一次随机时判断list是否Contains新随机出来的数字。。如果为false。。添加到list中。。反之再随机
      

  9.   


       List<Int32> list = new List<Int32>();
                while(true)
                {
                    Random r = new Random();
                        int n = r.Next(1,70);
                if(!list.Contains(n))
                {
                  list.Add(n);
                }
                if (list.Count > 20)
                {
                    break;
                }            }
    //这个是输出20个1-70的不重复的数啊,没有重复的