Random algorithm of 300,000 employees to extract 100,000 employees (shuffle algorithm)

Source: Internet
Author: User
Tags shuffle


Algorithm aspect is not my strength, so encountered this problem, record the solution;


At the beginning, the practice is to randomly select one from the random, and then determine whether there are existing lists,

And then repeat, see here, will algorithm students will certainly say this obviously not, just like how many people's birthday is more than 50% of the probability of a day;


So then find some information, see the shuffle algorithm, including a variety of varieties bar.


Here is the solution that I refer to;


For example, there are 300,000 elements of the list, then the first random selection of one; record position is x;

Then the element's last position element is stored in position x, then the element of the last position is deleted;

(here in exchange, it is necessary to determine whether this element was originally the last element);

Then randomly select one of the remaining elements so that it does not repeat;

Swap the last element to reduce the performance impact of the list's overall offset;


Everyone can try;

Random algorithm of 300,000 employees to extract 100,000 employees (shuffle algorithm)

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.