Results 1 to 2 of 2

Thread: Need a weighted random content generator

  1. #1
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need a weighted random content generator

    Removed cause I put it in the wrong area...sorry!
    Tim
    Last edited by tbrand68; 09-09-2009 at 11:50 AM. Reason: Put it in the wrong spot...

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function Shuffle(id,cls){
     var p=document.getElementById(id);
     var objs=zxcByClassName(cls,p)
     for (var ary=[],z0=0;z0<objs.length;z0++){
      ary.push([objs[z0],Math.random()*(objs[z0].className.replace(/\D/g,'')||1)]);
     }
     ary=ary.sort(function(a,b){return a[1]-b[1]; });
     for (var z1=0;z1<ary.length;z1++){
      p.appendChild(ary[z1][0]);
     }
    }
    
    function zxcByClassName(nme,el,tag){
     if (typeof(el)=='string') el=document.getElementById(el);
     el=el||document;
     for (var tag=tag||'*',reg=new RegExp('\\b'+nme+'\\b'),els=el.getElementsByTagName(tag),ary=[],z0=0; z0<els.length;z0++){
      if(reg.test(els[z0].className)) ary.push(els[z0]);
     }
     return ary;
    }
    
    /*]]>*/
    </script>
    </head>
    
    <body>
    <div id="tst" >
    <div class="zxc 10" >1</div>
    <div class="zxc 22" >2</div>
    <div class="zxc 15" >3</div>
    </div>
    <input type="button" name="" value="Shuffle" onclick="Shuffle('tst','zxc');"/>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

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
  •