How to generate non-repeating random numbers in. Net

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.