Results 1 to 3 of 3

Thread: Need a weighted random list generator.

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

    Default Need a weighted random list generator.

    I posted this yeterday in a different category...but I think I put it somewhere I should have. Sorry... So here it is again in hopefully the right forum topic.
    __________________________________________________

    Ok, so I've started looking at a script that dynamic drive wrote...
    http://www.dynamicdrive.com/dynamici...ntentorder.htm

    It does exactly what I need, except I need to weight certain items so they pop up to the top more often.

    I know it sounds odd to have random content show up in a not all random form.

    I've searched the archives for a script to do what I need, but I can't seem to find one. If there is one already, I appologize, if you could point me in the right direction, I'dd appreciate it.

    Otherwise, if you could help me modify the script above to add a weight element... I was thinking that in the class name, there could be an addition.. for example <div class="group1"> could be changed to <div class="group1,10"> where 10 would be the weight... the higer the number, the more often it would come up to the #1 position...

    This make sense??? sorry, I'm not a coder, just a dreamer! Any help would be appreciated.

    Regards,
    Tim

  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/

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

    tbrand68 (09-09-2009)

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

    Default

    Vic, looks like exactly what I need.

    Thank you so much for the quick response!

    Tim

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
  •