C # generates non-repeating random numbers

Source: Internet
Author: User

1         //produces n random numbers from min to max consecutive integers2         Private  Static int[] Generatenumbers (intMinintMaxintN)3         {4             //seed Array Length5             intSeednum = Max-min +1;6             //seed Array7            int[] Startarray =New int[Seednum];8            9             for(inti =0; i < Seednum; i++)Ten            { OneStartarray[i] = min++; A            } -             //a random number of numbers -             int[] Resultarray =New int[n];//the results are stored inside . theRandom rnd =NewRandom (); -              for(inti =0; I < n; i++) -             { -                 intSeed = rnd. Next (0, startarray.length-i);//generate from the rest of the random numbers. +Resultarray[i] = Startarray[seed];//Assigning a value to an array of results -                 //Replace the position where the random number was created with the unchecked value.  +Startarray[seed] = startarray[startarray.length-i-1]; A             } at  -              returnresultarray; -}
View Code

C # generates non-repeating random numbers

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.