produces an int array with a length of 100 and a random insertion of 1-100 into it, and cannot be repeated

Source: Internet
Author: User

Write in front

The day before yesterday to interview, given the written test has such an algorithm problem, an int array, the length of 100, and randomly inserted into the 1-100, and can not be repeated

At that time, the brain a hot, and did not think so much, with the collection to achieve a bit, the interviewer reminded, found there is a better way to achieve.

Code

First look at this piece of code

1 namespaceWolfy.randomdemo2 {3     class Program4     {5         Static voidMain (string[] args)6         {7list<int> LST =Newlist<int>();8Random r =NewRandom ();9              while(true)Ten             { One                 inttemp = R.next (1,101); A                 if(LST.) Count = = -) -                 { -                      Break; the                 } -                 if(!lst. Contains (temp)) -                 { - lst. ADD (temp); +                 } -             } +              for(inti =0; I < LST. Count; i++) A             { at Console.WriteLine (Lst[i]); -             } - Console.read (); -         } -     } -}

Although the above code, the implementation of the requirements of the topic, but if it is 1 to 1 million or greater, such a decision whether or not to include such a number, it is bound to affect performance.

Find a better way to implement online:

(1) Put n number in container a (int array).

(2) Randomly remove 1 numbers from the n number into container B (int array).

(3) to swap the last number in container A with the number of randomly extracted or to overwrite the last number in container a by randomly extracting the number.

(4) A random number is taken from container a (assuming N number, index 0 to index N-2). Then put in container B and repeat this step.

Note: That is, the second time is to randomly take a number from the first element in container A to the second-to-last element.

The benefit is that the range of random numbers is reduced gradually, and the bottleneck that occurs when a collection performs a delete operation is eliminated when big data is taken.

Code implementation

1 namespaceWolfy.randomdemo2 {3     class Program4     {5         Static voidMain (string[] args)6         {7             int[] result = Getrandom ( -);8              for(inti =0; I < result. Length; i++)9             {Ten Console.WriteLine (Result[i]); One             } AConsole.WriteLine ("Over :"+result. Length); - Console.read (); -         } the         /// <summary> -         ///get a non-repeating random array -         /// </summary> -         /// <param name= "n" >Upper Limit N</param> +         /// <returns>returns a random array</returns> -         Static int[] Getrandom (intN) +         { A             //containers A and B at             int[] Arrya =New int[n]; -             int[] Arryb =New int[n]; -             //Fill Container a -              for(inti =0; i < arrya.length; i++) -             { -Arrya[i] = i +1; in             } -             //Random Objects toRandom r =NewRandom (); +             //the index of the last element, such as n=100,end=99 -             intEnd = N-1; the              for(inti =0; I < n; i++) *             { $                 //generate random number because random is index so from 0 to 100 take, end=100Panax Notoginseng                 //A 32-bit signed integer greater than or equal to minValue and less than MaxValue, that is: the range of values returned includes MinValue but not maxValue.  -                 //if MinValue equals MaxValue, the MinValue is returned the                 // +                 intMinValue =0; A                 intMaxValue = end +1; the                 intRanindex =R.next (MinValue, maxValue); +                 //Put the random number in container b -Arryb[i] =Arrya[ranindex]; $                 //overwrite the removed element with the last element $Arrya[ranindex] =Arrya[end]; -                 //reduce the range of random number generation -end--; the             } -             //returns the generated random arrayWuyi             returnArryb; the         } -     } Wu}
Summarize

There are many ways to implement them, but they can be implemented in an efficient way, in an efficient manner. This generates no repetition of random numbers that can be used in a lottery system.

produces an int array with a length of 100 and a random insertion of 1-100 into it, and cannot be repeated

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.