熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

獲取不重復隨機數

2022-06-13   來源: .NET編程 

  static void Main(string[] args)

  {

  int[] myNum = new int[];

  myNum = GetRand( );

  ArraySort(myNum);

  foreach (int item in myNum)

  {

  ConsoleWriteLine(item);

  }

  ConsoleReadKey();

  return;

  }

  public static int[] GetRand(int minValue int maxValue int count)

  {

  maxValue = maxValue minValue + ;

  int[] arrInt = new int[maxValue];

  for (int i = ; i < maxValue; i++)

  {

  arrInt[i] = i + minValue;

  }

  int[] arrResult = new int[count];

  int n = maxValue;

  Random rand = new Random();

  for (int i = ; i < count; i++)

  {

  int index = randNext( n);

  arrResult[i] = arrInt[index];

  arrInt[index] = arrInt[n];

  }

  return arrResult;

  }


From:http://tw.wingwit.com/Article/program/net/201311/12886.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.