Methods for generating non-repeating random numbers in. Net
//gets count of integers that are not greater than maxnumber, and all integers are not duplicated. Of course, count must be less than or equal to maxnumber static list<int> Getrandomarray (Int maxnumber,int count) { List<int> list = new List<int> ();//Save the retrieved random number int[] array=new int[maxnumber];//defining the initial array for (int i = 0; i < maxnumber; i++)//assigning values to array elements array[i] = i + 1; random rnd = new random (); for (int j = 0; j < count; j++) { int index = rnd. Next (J,maxnumber);//Generate a random number as an array subscript int temp = array[index];//the number of index to subscript from the array list. Add (temp);//Adds the removed number to the list array[index] = array[j];//switch the number of subscript J to index position array[j] = temp;//swap the number of removed to the position of J } return list; }
Reference: Generating non-repeating random numbers in. NET http://www.studyofnet.com/news/977.html
How to generate non-repeating random numbers in. Net