在 Safari 中,我认为 IE7 和 8 Math.random() 也不是随机的?

发布于 2024-12-08 05:40:29 字数 977 浏览 1 评论 0原文

http://koreanwordgame.com/

该页面首先通过 Ajax 将 4 个单词加载到选项 DIV 中,然后随机化正确答案使用以下函数,传递包含要随机化的元素的 DIV 作为参数:

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>

问题是在 Safari 中,正确答案始终位于顶部位置...

而在 IE 7 和 8 中,它更经常位于顶部位置比 不是。

我知道可以通过使用时间戳或其他东西来使函数“更加随机”,但我正在努力使其正常工作。

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 DIV containing the elements to be randomized as the 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 is that in Safari the correct answer is always in the top position...

And in IE 7 and 8 it's in the top position far more often than not.

I know it would be possible to make the function "more random" by using a timestamp in there or something but I'm struggling to make it work properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别闹i 2024-12-15 05:40:29

问题不在于 Math.random()。这是您的“随机”排序函数,它只返回01,而从不返回-1。以下是如何在区间 [-1 中正确生成随机 int。 1],基于 MDC getRandomInt 函数:(

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

getRandomInt(-1, 1) 简化而来)。


也就是说,正如@32bitkid评论的那样,正确的方法是使用 Fischer-Yates 洗牌

The problem isn't Math.random(). It's your "randomizing" sort function, which only ever returns 0 or 1, and never -1. Here's how to properly generate a random int in the interval [-1. 1], based on MDC's getRandomInt function:

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

(simplified from getRandomInt(-1, 1)).


That said, as @32bitkid commented, the right way is to use a Fischer-Yates shuffle.

香草可樂 2024-12-15 05:40:29

您遇到的部分问题是如何使用随机函数。排序函数采用另一个函数,用于比较列表中的任意两个元素。排序函数做出的假设之一是任意两个元素之间的比较结果既稳定又一致(即,如果 a < b 且 b < c,则 a < c),并且您函数违反了该假设。因此,实际上无法保证排序函数在这种情况下会做什么。根据排序函数的实现方式(我不知道 JavaScript 中使用哪种算法),您很可能不会得到真正随机的顺序,其中每个项目同样可能出现在任何给定位置。

我能想到的最简单的解决方案是为要排序的每个项目生成一个随机数,然后按该随机生成的数字对项目进行排序。这与您的方法不同,因为在对项目进行排序时,比较列表中的任何两个元素将始终产生相同的结果。

Part of the problem you're having is how you're using the random function. The sort function takes another function that is used to compare any two elements in the list. One of the assumptions made by the sort function is that the results of the comparison between any two elements is both stable and consistent (i.e., if a < b and b < c, then a < c), and you're function violates that assumption. So, there's really no guarantees as to what the sort function will do in this case. Depending upon how the sort function is implemented (I don't know which algorithm is used in javascript off hand), you will most likely not get a truely random order where each item is equally likely to appear in any given position.

The simplest solution I can think of is to generate a random number for each item you want to sort, and then sort the items by that randomly generated number. This is different from your approach in that comparing any two elements in your list will always produce the same result while the items are being sorted.

感受沵的脚步 2024-12-15 05:40:29

return( temp%2 ); 只剩下 01

尝试使用 return( temp%4 ); 这会给你留下 0,1,2,3

虽然我不确定模数是多少对于。
这将为您提供 0 到 3 之间的随机数:

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

所以您需要做的就是:

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

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);            
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文