{"id":7996,"date":"2018-06-15T11:48:02","date_gmt":"2018-06-15T11:48:02","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=7996"},"modified":"2022-01-05T09:45:38","modified_gmt":"2022-01-05T09:45:38","slug":"using-a-shuffle-algorithm-on-a-deck-of-cards","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/using-a-shuffle-algorithm-on-a-deck-of-cards\/","title":{"rendered":"Using a Shuffle Algorithm on a Deck of Cards"},"content":{"rendered":"<h3><strong>Shuffle a Deck of Cards<\/strong><\/h3>\n<p>Let\u2019s try to write a method which will shuffle a deck of cards. The deck should be perfectly mixed. The card permutations should be equally probable.<\/p>\n<p><strong>Solution<\/strong><\/p>\n<p>We will use the perfect random number generator. Let\u2019s solve the problem using the most obvious way. We can select random cards and put them into a new deck. In fact, a deck is an array; therefore, we need a way to lock some particular elements.<\/p>\n<p>Original deck [1] [2] [3] [4] [5]<\/p>\n<p>\/* Choose a random element to put it in the beginning of the shuffled deck *\/<br \/>\n\/* Mark it in the original deck as &#8220;locked&#8221; so we will not take it again *\/<\/p>\n<p>Shuffled deck [4] [?] [?] [?] [?]<br \/>\nOriginal deck [1] [2] [3] [X] [5]<\/p>\n<p><!--more--><br \/>\nIf we mark item [4] will that prevent selecting it again? One way is to swap the &#8220;locked&#8221; ([4]) and the first elements of the deck:<\/p>\n<p>Original deck [1] [2] [3] [4] [5]<\/p>\n<p>\/* Choose a random element to put it in the beginning of the shuffled deck and<br \/>\nchange the current element with the 1st one *\/<\/p>\n<p>Shuffled deck [4] [?] [?] [?] [?]<br \/>\nOriginal deck [X] [2] [3] [1] [5]<\/p>\n<p>\/* Choose a random element to put it in the beginning of the shuffled deck<br \/>\nand change the current element with the 2nd one *\/<\/p>\n<p>Shuffled deck [4] [3] [?] [?] [?]<br \/>\nOriginal deck [X] [X] [2] [1] [5]<\/p>\n<p>The algorithm is easier to implement for the situation where the first k cards are &#8220;locked&#8221; than for the situation where, for example, the third, fourth, and ninth cards are &#8220;locked&#8221;.<\/p>\n<p>We can optimize the algorithm combining the shuffled and the original deck.<\/p>\n<p>Original deck [1] [2] [3] [4] [5]<\/p>\n<p>\/* Choose a random element to put it between 1 and 5 and swap it with 1.<br \/>\nIn this example we chose 4th;<br \/>\nafter that, the 1st element is locked *\/<\/p>\n<p>Original deck [4] [2] [3] [1] [5]<\/p>\n<p>\/* Element 1 is locked. Choose a random element to swap it with element 2.<br \/>\nIn this case it will be number 3*\/<\/p>\n<p>Original deck [4] [3] [2] [1] [5]<br \/>\n\/* repeat *\/<\/p>\n<p>This algorithm is easy to implement iteratively:<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\npublic void shuffle(int[] array) {<br \/>\n    int tmp, number;<br \/>\n    for (int i = 0; &amp;lt; array.length; i++) {<\/p>\n<p>    number = (int) (Math.random() * (array.length &#8211; i)) + i;<br \/>\n    tmp = array[i];<br \/>\n        array[i] = array[number];<br \/>\n        array[number] = tmp;<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Thanks for your attention!<\/p>\n<blockquote><p><em>This sample with a shuffle algorithm can also be used to deal with your own homework. Future programmers will find this guide useful. Now we have come very close to your problem of not knowing how to deal with your assignment. AssignmentShark.com represents a service where you can get qualified help. We help students from all over the world, so it doesn\u2019t matter what country you are from. What happens if you use our service? Or, is it a good idea to place an order on our site?<\/em><\/p>\n<p><em>By ordering, you will get a completed assignment without any errors. Since it is necessary to hand in your assignment on time, our experts work fast so that you will get a completed order as soon as possible. Our expert will take care of your order and complete it according to your requirements and academic standards. Place an order on our site if any assignment problem occurs. We can protect you from failures in studying. <a href=\"https:\/\/assignmentshark.com\/do-my-computer-science-homework.html\" target=\"_blank\" rel=\"noopener\">Do my computer science assignment<\/a> &#8211; Easily!<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Shuffle a Deck of Cards Let\u2019s try to write a method which will shuffle a deck of cards. The deck should be perfectly mixed. The card permutations should be equally probable. Solution We will use the perfect random number generator. Let\u2019s solve the problem using the most obvious way. We can select random cards and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-7996","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/7996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=7996"}],"version-history":[{"count":6,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/7996\/revisions"}],"predecessor-version":[{"id":12839,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/7996\/revisions\/12839"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=7996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=7996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=7996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}