I am trying to make divs appear in random order everytime the page is loaded.
I implemented the following code:
<script type="text/javascript">
$(function() {
$(window).load(function(){
$("div.container").randomize("div.random");
});
});
(function($) {
$.fn.randomize = function(childElem) {
return this.each(function() {
var $this = $(this);
var elems = $this.children(childElem);
elems.sort(function() { return (Math.round(Math.random())-0.8); });
$this.remove(childElem);
for(var i=0; i < elems.length; i++)
$this.append(elems[i]);
});
}
})(jQuery);
</script>
<div class="container">
<div class="random"> 1 </div>
<div class="random"> 2 </div>
<div class="random"> 3 </div>
<div class="random"> 4 </div>
<div class="random"> 5 </div>
<div class="random"> 6 </div>
<div class="random"> 7 </div>
<div class="random"> 8 </div>
</div>
Divs really change their positions/order everytime the page is loaded. However, statistically - "1" tends to appear almost always in the "top 4", on the contrary "8" is almost always at the bottom of the list!
That does not seem like being really RANDOM... I will highly appreciate any suggestions about this. It is very important that every div has the same chance to be on top of the list.