Results 1 to 5 of 5

Thread: Increase Randomness of Random Content Order script

  1. #1
    Join Date
    Mar 2010
    Location
    t-dot
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Increase Randomness of Random Content Order script

    1) Random Content Order script

    2) http://www.dynamicdrive.com/dynamici...ntentorder.htm

    Hi, I'm actually using the version that was modified to change the content periodically. It works exactly as advertised.

    My Question is in regards to the randomness of the content.

    When you look at the example or use just a few table rows, it appears to be totally random, but if you have 20 rows in your table it starts to appear as if the same row will appear at or near the top for quite a few times until it goes near the bottom, in other words the randomness seems not so random.

    I wonder if there is a way to make it "more random", or do you think that my observation is just skewed or not accurate.

    I've set the cookie to "0" so that the change is session based for testing. I also use the javascript kill cookie command
    Code:
    javascript:void(randomizeContent.cookie.kill('group1'))

    Thanks for any thoughts you might have.

    jim
    Last edited by jimlongo; 02-29-2012 at 05:16 AM. Reason: removed URL of test page

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Try this updated version of the script:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Greater Random Content w/cookies - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
    .group1 {
    visibility: hidden;
    }
    </style>
    <noscript>
    <style type="text/css">
    .group1 {
    visibility: visible;
    }
    </style>
    </noscript>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    
    <script type="text/javascript">
    
    /***********************************************
    * Random Content Order script- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    * Modified 03/04/2010 cookie persistence by jscheuer1 in http://www.dynamicdrive.com/forums/
    * Modified 02/28/2012 dual cookie persistence for greater randomness by jscheuer1 in http://www.dynamicdrive.com/forums/
    ***********************************************/
    
    function randomizeContent(classname, days){
    	var $ = jQuery, contents = {
    		ref: $('.' + classname),
    		text: [],
    		cookie: randomizeContent.cookie.get(classname)? randomizeContent.cookie.get(classname).split('.') : [],
    		longterm: randomizeContent.cookie.get(classname + 'long')? randomizeContent.cookie.get(classname + 'long').split('.') : [],
    		set: function(){
    			randomizeContent.cookie.set(classname, contents.cookie.join('.'), days || 0);
    			randomizeContent.cookie.set(classname + 'long', contents.cookie.join('.'), 365);
    		}
    	};
    	contents.ref.each(function(i){
    		contents.text[i] = [this.innerHTML, i];
    	});
    	if(contents.cookie.length === 0 && contents.longterm.length === 0){
    		contents.text.sort(function(){return 0.5 - Math.random();});
    		contents.ref.each(function(i){
    			$(this).html(contents.text[i][0]).css('visibility', 'visible');
    			contents.cookie[i] = contents.text[i][1];
    		});
    		contents.set();
    	} else if (contents.cookie.length === 0 && contents.longterm.length === contents.text.length){
    		contents.longterm.sort(function(){return 0.5 - Math.random();});
    		contents.ref.each(function(i){
    			$(this).html(contents.text[contents.longterm[i]][0]).css('visibility', 'visible');
    			contents.cookie[i] = contents.text[contents.longterm[i]][1];
    		});
    		contents.set();
    	} else {
    		contents.ref.each(function(i){
    			$(this).html(contents.text[contents.cookie[i]][0]).css('visibility', 'visible');
    		});
    	}
    }
    
    randomizeContent.cookie = {
    	set: function(n, v, d){ // cookie.set takes (name, value, optional_persist_days) - defaults to session if no days specified
    		if(d){var dt = new Date(); 
    		dt.setDate(dt.getDate() + d);
    		d = '; expires=' + dt.toGMTString();}
    		document.cookie = n + '=' + escape(v) + (d || '') + '; path=/';
    	},
    	get: function(n){ // cookie.get takes (name)
    		var c = document.cookie.match('(^|;)\x20*' + n + '=([^;]*)');
    		return c? unescape(c[2]) : null;
    	},
    	kill: function(n){ // cookie.kill takes (name)
    		randomizeContent.cookie.set(n, '', -1);
    	}
    };
    </script>
    </head>
    <body>
    <div class="group1">
    Content 1
    </div>
    
    <div class="group1">
    Content 2
    </div>
    
    <div class="group1">
    Content 3
    </div>
    
    <div class="group1">
    Content 4
    </div>
    
    <div class="group1">
    Content 5
    </div>
    
    <input type="button" value="Kill" onclick="randomizeContent.cookie.kill('group1');"><br>
    <input type="button" value="Kill Long" onclick="randomizeContent.cookie.kill('group1long');"><br>
    <input type="button" value="Check" onclick="alert(document.cookie);">
    <script type="text/javascript">
    
    //randomize order of contents with DIV class="group1" to persist for 1 day (use 0 for session only persistence)
    randomizeContent('group1', 1);
    
    </script>
    </body>
    </html>
    Notes: You will have to run:

    Code:
    javascript:void(randomizeContent.cookie.kill('group1'))
    as I've changed the separator in the cookie. If you don't kill the old one, you will get a persistent error.

    Other changes in this version:

    • Added a noscript tag with a style section which makes the content visible in its normal order for users without javascript enabled.

    • Added a second long term cookie. If the short term cookie has expired or been killed, the long term cookie still holds the previous random order. Instead of randomizing the order of the elements on the page, this long term order is shuffled. This leads to a greater degree of true randomness.


    I hit on this method after having helped someone with a game where items had to be picked randomly and repeatedly over the course of the game. By constantly shuffling the order instead of starting from the same order for each shuffle, a high degree of true randomness was achieved.

    However, you might not notice it in a small sample. That's just the nature of randomness. You can get the same or nearly the same result repeatedly because random only guarantees that over a large sample the results will fall nearly equally into whatever the various categories are.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    jimlongo (02-29-2012)

  4. #3
    Join Date
    Mar 2010
    Location
    t-dot
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks John, that's great and it does appear to be "more random".

    Question about the buttons, Kill seems straightforward, what is Kill Long and Check?

    Also when I first try the page of course it doesn't load properly because of the existing cookie, I assume that's not going to be a problem for first time visitors.

    Thanks again,
    jim

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    No, not a problem for first time visitors. And anyone who had visited before having a problem can simply clear their cookies to fix it, the kill function is not required for that, it's just a convenience. And if their cookie has expired, there will be no problem either. It was set to expire at the end of the session, right? If so, nothing to worry about.

    The buttons are just for utility/diagnostic purposes and need not be used in the live version.

    Kill Long will erase the long term cookie, and Check will alert all current cookies for the page. Usually the cookies for this script will appear at the end of that list. In some browsers, if the list is too long, it will be truncated.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #5
    Join Date
    Mar 2010
    Location
    t-dot
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Hi John, sometimes the cookies seem to be pretty persistent even though they're set to zero.

    I can quit and restart a few times in some cases before they seem to expire. I'm not really too concerned except where I've added an additional div or list item (which can be often), and this will render a blank or incomplete page.

    Any thoughts on that?

    http://tennislessonsintoronto.com/junior
    http://tennislessonsintoronto.com/adult
    http://tennislessonsintoronto.com/summercamps
    http://tennislessonsintoronto.com/resources

    Thanks,
    Jim

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •