1

假设Math.random()产生 0 和 1 之间均匀分布的随机数,这是 Fischer Yates shuffle 的正确实现吗?我正在寻找一个非常随机、均匀的分布,其中arr可以指定输入数组 ( ) 中的混洗元素的数量 (as required)。

shuffle = (arr, required)->
  rnd = (int) ->
    r = Math.random() * int
    Math.round r

  len = arr.length-1

  for i in [len..1]
    random = rnd(i)
    temp = arr[random]
    arr[random] = arr[i]
    arr[i] = temp
    break if i < len - (required - 2)

  return arr
4

1 回答 1

1

几件事:

  • 而不是Math.round(),尝试Math.floor();在您的实现Math.round()中,第一个元素(在索引 0 处)和最后一个元素比所有其他元素(.5/len vs. 1/len)的机会更少。请注意,在第一次迭代中,您输入arr.length - 1元素arr.length
  • 如果你有一个required 变量,你也可以让它成为可选的,因为它默认为数组的长度:shuffle = (arr, required=arr.length)
  • 即使您只打乱了最后一个元素,您也会返回整个数组。考虑改为返回arr[arr.length - required ..]
  • 如果required不在范围内[0,arr.length]怎么办?

将它们放在一起(并添加一些天赋):

    shuffle = (arr, required=arr.length) ->
      randInt = (n) -> Math.floor n * Math.random()
      required = arr.length if required > arr.length
      return arr[randInt(arr.length)] if required <= 1

      for i in [arr.length - 1 .. arr.length - required]
        index = randInt(i+1)
        # Exchange the last unshuffled element with the 
        # selected element; reduces algorithm to O(n) time
        [arr[index], arr[i]] = [arr[i], arr[index]]

      # returns only the slice that we shuffled
      arr[arr.length - required ..]

    # Let's test how evenly distributed it really is
    counter = [0,0,0,0,0,0]
    permutations = ["1,2,3","1,3,2","2,1,3","2,3,1","3,2,1","3,1,2"]
    for i in [1..12000]
      x = shuffle([1,2,3])
      counter[permutations.indexOf("#{x}")] += 1

    alert counter
于 2012-10-18T06:20:44.813 回答