Random shuffling algorithm and random selection algorithm

Source: Internet
Author: User

1. Random shuffling Algorithm

Problem description: sorts the elements in an ordered set in random order.

Solution:

For I: = 1 to n do swap (A [I], a [random (1, N)]); // merge, but not really random

For
I: = 1 to n do swap (A [I], a [random (I, n)]); // real random Algorithm

2. Random selection algorithm

How to extract M random numbers from N

Solution: the probability of the first number being extracted is 1, the second is 1/2, the third is 1/3, and so on, the nth number is 1/N, the probability of each number being extracted is 1/N.

The first M count is the M count at initialization. When the nth number is reached, the random number is located in 1 ~ N. If the random number is in m, replace the number and the random number until all data is traversed. The probability of each number being extracted is M/N.


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.