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

前端 未结 3 615
灰色年华
灰色年华 2021-01-26 06:17

http://koreanwordgame.com/

This page first loads 4 words into the options DIVs via Ajax and then randomizes the correct answer with the following function, passing the D

3条回答
  •  悲&欢浪女
    2021-01-26 07:06

    return( temp%2 ); Leaves you with only 0 or 1

    Try using return( temp%4 ); Which would leave you with 0,1,2,3

    Although I am not sure what the modulus is for.
    This will get you a random number between 0 and 3:

    Math.floor(Math.random()*4)

    So all you need to do is:

    var random = function(r){
        r.children().sort(function(a,b){
            return Math.floor(Math.random()*4);
        }).appendTo(r);            
    };
    

提交回复
热议问题