javascript - In Safari and I think also IE7 and 8 Math.random() is NOT random? -


http://koreanwordgame.com/

this page first loads 4 words options divs via ajax , randomizes correct answer following function, passing div containing elements randomized argument:

var random = function(r){     r.children().sort(function(a,b){       var temp = parseint( math.random()*10 );       return( temp%2 );     }).appendto(r);             };  random($("#option"));  <div id="option">    <div class="option" id="option1" style="background-color: rgb(229, 232, 238); ">light</div> <div class="option" id="option4" style="background-color: rgb(183, 190, 204); ">pot</div> <div class="option" id="option2" style="background-color: rgb(183, 190, 204); ">garlic press</div> <div class="option" id="option3" style="background-color: rgb(183, 190, 204); ">habitant</div> </div> 

the problem in safari correct answer in top position...

and in ie 7 , 8 it's in top position far more not.

i know possible make function "more random" using timestamp in there or i'm struggling make work properly.

the problem isn't math.random(). it's your "randomizing" sort function, ever returns 0 or 1, , never -1. here's how generate random int in interval [-1. 1], based on mdc's getrandomint function:

math.floor(math.random() * 3) - 1; 

(simplified getrandomint(-1, 1)).


that said, @32bitkid commented, right way use fischer-yates shuffle.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -