Log in

View Full Version : Need a weighted random list generator.



tbrand68
09-09-2009, 11:48 AM
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

vwphillips
09-09-2009, 04:12 PM
<!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>

tbrand68
09-09-2009, 04:42 PM
Vic, looks like exactly what I need.

Thank you so much for the quick response!

Tim