Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <!--
- Here's a client-side solution. Holler, if you'd prefer a server-side one.
- -->
- <script>
- // define randomization object to encapsulate functions
- var Randomizer = {
- // get random integer in range [min, max] - min is optional with default 0
- getRandomInt: function(max, min) {
- if ('undefined' == typeof min) min = 0;
- // reject invalid arguments
- if (max <= min || min < 0 || max < 0) return null;
- return min + Math.floor(Math.random()*(max - min + 1));
- },
- // draw without replacement cnt values from range [min, max]
- drawRandomInts: function(cnt, max, min) {
- if ('undefined' == typeof min) min = 0;
- if ('undefined' == typeof max) max = cnt - 1;
- // reject request for more integers than exist in range
- if (cnt > max - min + 1) return null;
- var hash = new Array();
- var drawn = 0;
- while (cnt > drawn) {
- var r = '' + this.getRandomInt(max, min);
- // exit immediately for bad result
- if (null == r) return null;
- if ('undefined' == typeof hash[r]) {
- hash[r] = drawn++;
- }
- }
- var ints = new Array();
- var i = 0;
- for (key in hash) {
- ints[i] = parseInt(hash[key]);
- i++;
- }
- return ints;
- },
- // randomly rearrange the elements of an array
- shuffle: function(arr) {
- var idxs = this.drawRandomInts( arr.length );
- // exit immediately for bad result
- if (null == idxs) return null;
- var last = arr.length;
- var shuffled = new Array();
- for (var i = 0; i < last; i++ ) {
- shuffled.push( arr[ idxs[ i ]] );
- }
- return shuffled;
- }
- };
- // declare array of quotations
- var quotes = [
- "I would never die for my beliefs because I might be wrong.",
- "The only thing that saves us from the bureaucracy is inefficiency. An efficient bureaucracy is the greatest threat to liberty.",
- "Insanity: doing the same thing over and over again and expecting different results.",
- "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."
- ];
- // initialize index position beyond end of array of quotes
- var quoteIdx = quotes.length;
- // retrieve a single, randomly selected quote
- function getQuote( ) {
- // increment the index position to use
- quoteIdx++;
- // if index is out of bounds...
- if (quotes.length <= quoteIdx) {
- // randomly rearrange the quotes
- quotes = Randomizer.shuffle( quotes );
- // reset index to first position
- quoteIdx = 0;
- }
- // return current quote by index position
- return quotes[ quoteIdx ];
- };
- // optionally could do this onload
- window.onload = function() {
- alert( getQuote() );
- };
- </script>
- </head>
- <body>
- <p>
- 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.
- </p>
- <form>
- <p>
- <input type="button" value="get quote" onclick="alert( getQuote() );" />
- </p>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement