Advertisement
richarduie

randomQuote.html

Jun 1st, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.63 KB | None | 0 0
  1. <html>
  2. <head>
  3.     <!--
  4.         Here's a client-side solution. Holler, if you'd prefer a server-side one.
  5.     -->
  6.  
  7.     <script>
  8.         // define randomization object to encapsulate functions
  9.         var Randomizer =  {
  10.             // get random integer in range [min, max] - min is optional with default 0
  11.             getRandomInt: function(max, min) {
  12.                 if ('undefined' == typeof min) min = 0;
  13.                 // reject invalid arguments
  14.                 if (max <= min || min < 0 || max < 0) return null;
  15.                 return min + Math.floor(Math.random()*(max - min + 1));
  16.             },
  17.             // draw without replacement cnt values from range [min, max]
  18.             drawRandomInts: function(cnt, max, min) {
  19.                 if ('undefined' == typeof min) min = 0;
  20.                 if ('undefined' == typeof max) max = cnt - 1;
  21.                 // reject request for more integers than exist in range
  22.                 if (cnt > max - min + 1) return null;
  23.                 var hash = new Array();
  24.                 var drawn = 0;
  25.                 while (cnt > drawn) {
  26.                     var r = '' + this.getRandomInt(max, min);
  27.                     // exit immediately for bad result
  28.                     if (null == r) return null;
  29.                     if ('undefined' == typeof hash[r]) {
  30.                         hash[r] = drawn++;
  31.                     }
  32.                 }
  33.                 var ints = new Array();
  34.                 var i = 0;
  35.                 for (key in hash) {
  36.                     ints[i] = parseInt(hash[key]);
  37.                     i++;
  38.                 }
  39.                 return ints;
  40.             },
  41.             // randomly rearrange the elements of an array
  42.             shuffle: function(arr) {
  43.                 var idxs = this.drawRandomInts( arr.length );
  44.                 // exit immediately for bad result
  45.                 if (null == idxs) return null;
  46.                 var last = arr.length;
  47.                 var shuffled = new Array();
  48.                 for (var i = 0; i < last; i++ ) {
  49.                     shuffled.push( arr[ idxs[ i ]] );
  50.                 }
  51.                 return shuffled;
  52.             }
  53.         };
  54.  
  55.         // declare array of quotations
  56.         var quotes = [
  57.             "I would never die for my beliefs because I might be wrong.",
  58.             "The only thing that saves us from the bureaucracy is inefficiency. An efficient bureaucracy is the greatest threat to liberty.",
  59.             "Insanity: doing the same thing over and over again and expecting different results.",
  60.             "Don't you wish there was a knob on the TV to turn up the intelligence? There's one marked 'Brightness,' but it doesn't work."
  61.         ];
  62.        
  63.         // initialize index position beyond end of array of quotes
  64.         var quoteIdx = quotes.length;
  65.         // retrieve a single, randomly selected quote
  66.         function getQuote( ) {
  67.             // increment the index position to use
  68.             quoteIdx++;
  69.             // if index is out of bounds...
  70.             if (quotes.length <= quoteIdx) {
  71.                 // randomly rearrange the quotes
  72.                 quotes = Randomizer.shuffle( quotes );
  73.                 // reset index to first position
  74.                 quoteIdx = 0;
  75.             }
  76.             // return current quote by index position
  77.             return quotes[ quoteIdx ];
  78.         };
  79.  
  80.         // optionally could do this onload
  81.         window.onload = function() {
  82.             alert( getQuote() );
  83.         };
  84.     </script>
  85. </head>
  86. <body>
  87.     <p>
  88.         The button below provides a control to play with the quotes.<br><br> Note that every time you cycle through the full set of quotes, the array will be re-randomized. By randomly rearranging and then presenting the individual quotes in straight, serial order, i.e., 0, 1, 2,..., you're guaranteed not to repeat a quote until the set has to be reshuffled for a new cycle of presentation and possibly not even then. This contrasts what would be expected if the original order of declaration of the quotes array remained static, and the index position of the particular quote to display was randomly generated each time. In the latter approach, quotes could easily repeat, since the likelihood of selection would be the same for each quote for each display iteration.    
  89.     </p>
  90.     <form>
  91.         <p>
  92.             <input type="button" value="get quote" onclick="alert( getQuote() );" />
  93.         </p>
  94.     </form>
  95. </body>
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement