2010年12月31日 星期五

產生隨機數列

程式碼
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
 class Program
 {
  static void Main(string[] args)
  {
   foreach (int tt in GenerateRandomList(1, 20))
   {
    Console.WriteLine(tt);
   }
   Console.ReadLine();
  }

  private static List GenerateRandomList(int min,int max)
  {
   Random rand = new Random();
   int index = 0;
   string str = string.Empty;
   List list = new List();
   List randList = new List();

   for (int j = min; j <= max; j++)
   {
    list.Add(j);
   }

   for (int i = 0; i < max; i++)
   {
    index = rand.Next(0, list.Count - 1);
    randList.Add (list[index]);
    list.RemoveAt(index);
   }
   return randList ;
  }
 }
}

沒有留言: