{"id":27,"date":"2016-12-20T16:46:14","date_gmt":"2016-12-20T16:46:14","guid":{"rendered":"http:\/\/designedbyashw.in\/blog\/?p=27"},"modified":"2021-07-26T08:41:25","modified_gmt":"2021-07-26T08:41:25","slug":"randomize-questions","status":"publish","type":"post","link":"https:\/\/designedbyashw.in\/blog\/blog\/2016\/12\/20\/randomize-questions\/","title":{"rendered":"Randomize Questions"},"content":{"rendered":"\n<p>Recently i was faced with a problem wherein, given a list of question, the order in which they were displayed must vary every time it is presented in a webpage. In this article I will illustrate how we can use&nbsp;The <b>Fisher\u2013Yates shuffle<\/b>&nbsp;<a title=\"Algorithm\" href=\"https:\/\/en.wikipedia.org\/wiki\/Algorithm\">algorithm<\/a>&nbsp;to solve this problem and dynamically display the order of the questions<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">The Algorithm:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ To shuffle an array <i>a<\/i> of <i>n<\/i> elements (indices 0..n-1):\n<b>for<\/b> <i>i<\/i> <b>from<\/b> <i>n<\/i>\u22121 <b>downto<\/b> 1 <b>do<\/b>\n     <i>j<\/i> \u2190 random integer such that 0 \u2264 <i>j<\/i> \u2264 <i>i<\/i>\n     exchange <i>a<\/i>[<i>j<\/i>] and <i>a<\/i>[<i>i<\/i>]\n\n\/\/ -(Source Wikipedia)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Steps:<\/h2>\n\n\n\n<p>As we need to shuffle only certain items in the page, it would be wise to first group them together. Lets assign a class <code>.random<\/code>&nbsp;to the elements. Here we will also need to identify a parent container which will contain all the shuffled questions. <code>#question-container<\/code><\/p>\n\n\n\n<pre title=\"\" class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\"><meta charset=\"utf-8\">&lt;!-- index.html -->\n&lt;ul id=\"question-container\">\n &lt;li class=\"random\">Question 1&lt;\/li>\n &lt;li class=\"random\">Question 2&lt;\/li>\n &lt;li class=\"random\">Question 3&lt;\/li>\n &lt;li class=\"random\">Question 4&lt;\/li>\n&lt;\/ul><\/code><\/pre>\n\n\n\n<p>Now we need to select them in our scripts. We can use jQuery or plain old javascript to do this. Let us also remove the questions once selected from the DOM<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ shuffle.js\nvar questions = $(\".random\");\nvar container = $(\"#question-container\");\n\nquestions.remove();<\/code><\/pre>\n\n\n\n<p>Now that the questions don&#8217;t exist in the DOM and are selected, lets write the algorithm to shuffle them. We will use the&nbsp;<b>Fisher\u2013Yates shuffle<\/b>&nbsp;<a title=\"Algorithm\" href=\"https:\/\/en.wikipedia.org\/wiki\/Algorithm\">algorithm<\/a>&nbsp;to do this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ Fisher\u2013Yates shuffle algorithm\nfor (var i = questions.length - 1; i &gt; 0; i--) {\n var rand_int = getRandomInt(0, i);\n var temp = questions[i];\n questions[i] = questions[rand_int];\n questions[rand_int] = temp\n}<\/code><\/pre>\n\n\n\n<p>Now our questions array will contain all the questions in a shuffled manner. The <code>getRandom(a,b)<\/code>&nbsp;function is used to get a random number between a and b, inclusive of a and b. You can write the function as below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ Get random integer within a range inclusive of min and max values\n function getRandomInt(min, max) {\n min = Math.ceil(min);\n max = Math.floor(max);\n return Math.floor(Math.random() * (max - min + 1)) + min;\n }<\/code><\/pre>\n\n\n\n<p>Now all that is left to complete this is adding the questions back to the paretn container. We can do this using the <code>append()<\/code>&nbsp;function in jQuery (similar functions exist in javascript too).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">for (var i = 0; i &lt; questions.length; i++) {\n container.append(questions[i]);\n}<\/code><\/pre>\n\n\n\n<p>And we are done. The whole code for this is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">&lt;!-- index.html --&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n &lt;head&gt;\n   &lt;meta charset=\"utf-8\"&gt;\n   &lt;title&gt;Shuffle&lt;\/title&gt;\n &lt;\/head&gt;\n &lt;body&gt;\n   &lt;ul id=\"question-container\"&gt;\n     &lt;li class=\"random\"&gt;Question 1&lt;\/li&gt;\n     &lt;li class=\"random\"&gt;Question 2&lt;\/li&gt;\n     &lt;li class=\"random\"&gt;Question 3&lt;\/li&gt;\n     &lt;li class=\"random\"&gt;Question 4&lt;\/li&gt;\n   &lt;\/ul&gt;\n &lt;!-- Load Jquery --&gt;\n &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jquery\/3.1.1\/jquery.min.js\" charset=\"utf-8\"&gt;&lt;\/script&gt;\n &lt;script src=\"shuffle.js\" charset=\"utf-8\"&gt;&lt;\/script&gt;\n &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>and our script file as<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ shuffle.js\nvar questions = $(\".random\");\nvar container = $(\"#question-container\");\n\nquestions.remove();\n\n\/\/ Fisher\u2013Yates shuffle algorithm\nfor (var i = questions.length - 1; i &gt; 0; i--) {\n var rand_int = getRandomInt(0, i);\n var temp = questions[i];\n questions[i] = questions[rand_int];\n questions[rand_int] = temp\n}\n\nfor (var i = 0; i &lt; questions.length; i++) {\n container.append(questions[i]);\n}\n\n\n\/\/ Get random integer within a range inclusive of min and max values\n function getRandomInt(min, max) {\n   min = Math.ceil(min);\n   max = Math.floor(max);\n   return Math.floor(Math.random() * (max - min + 1)) + min;\n }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Cheers!<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Recently i was faced with a problem wherein, given a list of question, the order in which they were displayed must vary every time it&hellip;<\/p>\n","protected":false},"author":1,"featured_media":109,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[11],"_links":{"self":[{"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/posts\/27"}],"collection":[{"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":9,"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions\/306"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/media\/109"}],"wp:attachment":[{"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/designedbyashw.in\/blog\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}