PDA

View Full Version : Need a weighted random content generator



tbrand68
09-08-2009, 01:01 PM
Removed cause I put it in the wrong area...sorry!
Tim

vwphillips
09-09-2009, 04:14 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>