{"id":1626,"date":"2021-07-20T22:18:14","date_gmt":"2021-07-21T05:18:14","guid":{"rendered":"http:\/\/66.94.114.210\/?p=1626"},"modified":"2021-07-20T22:24:07","modified_gmt":"2021-07-21T05:24:07","slug":"fisher-yates-shuffle","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2021\/07\/20\/fisher-yates-shuffle\/","title":{"rendered":"Fisher\u2013Yates Shuffle"},"content":{"rendered":"\n<p>Generating a random permutation of a finite sequence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">O(n^2) <\/h2>\n\n\n\n<p>The basic method given for generating a random permutation of the numbers 1 through&nbsp;<em>N<\/em>&nbsp;goes as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Write down the numbers from 1 through\u00a0<em>N<\/em>.<\/li><li>Pick a random number\u00a0<em>k<\/em>\u00a0between one and the number of unstruck numbers remaining (inclusive).<\/li><li>Counting from the low end, strike out the\u00a0<em>k<\/em>th number not yet struck out, and write it down at the end of a separate list.<\/li><li>Repeat from step 2 until all the numbers have been struck out.<\/li><li>The sequence of numbers written down in step 3 is now a random permutation of the original numbers.<\/li><\/ol>\n\n\n\n<p>It is obvious that at each step, a number has equal possibility to be picked out. <\/p>\n\n\n\n<p>Or we can consider it in the other way: at each step the number we will pick will have <code>sizeof(remaining_list)<\/code> possibilities. So overall we will have <code>n!<\/code> <meta charset=\"utf-8\">possibilities.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">O(n)<\/h2>\n\n\n\n<pre id=\"block-a7437f71-559e-4c5b-8e34-5bb3665d273f\" class=\"wp-block-preformatted\">-- To shuffle an array <em>a<\/em> of <em>n<\/em> elements (indices 0..<em>n<\/em>-1):<br><strong>for<\/strong> <em>i<\/em> <strong>from<\/strong> 0 <strong>to<\/strong> <em>n<\/em>\u22122 <strong>do<\/strong><br>     <em>j<\/em> \u2190 random integer such that <em>i<\/em> \u2264 <em>j<\/em> &lt; <em>n<\/em><br>     exchange <em>a<\/em>[<em>i<\/em>] and <em>a<\/em>[<em>j<\/em>]<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">vector&lt;int> shuffle(vector&lt;int> const&amp; origin) {\n  vector&lt;int> res = origin;\n  for (int i = 0; i + 1 &lt; res.size(); ++i) {\n    int j = i + rand() % (res.size() - i);\n    swap(res[i], res[j]);\n  }\n  return res;\n}<\/code><\/pre>\n\n\n\n<p>The correctness is also obvious. We basically use the original array as the separate list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generating a random permutation of a finite sequence. O(n^2) The basic method given for generating a random permutation of the numbers 1 through&nbsp;N&nbsp;goes as follows: Write down the numbers from 1 through\u00a0N. Pick a random number\u00a0k\u00a0between one and the number of unstruck numbers remaining (inclusive). Counting from the low end, strike out the\u00a0kth number not&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,68],"tags":[],"class_list":["post-1626","post","type-post","status-publish","format-standard","hentry","category-alg","category-random"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1626"}],"version-history":[{"count":3,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1626\/revisions"}],"predecessor-version":[{"id":1630,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1626\/revisions\/1630"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}