与JavaScript
; a [i] = a [j]; a [j] = x; } 返回a; } 此算法以相反的顺序通过数组迭代,将每个元素与数组前面的随机元素交换。 The resulting array is shuffled due to the random nature of the swaps.
Usage
The shuffle function can be used to shuffle an array using the following syntax:
var myArray = ['1','2','3','4','5','6','7','8','9']; shuffle(myarray);es6版本
fisher-yates algorithm在es6:function shuffle(a) { var j, x, i; for (i = a.length - 1; i > 0; i--) { j = Math.floor(Math.random() * (i 1)); x = a[i]; a[i] = a[j]; a[j] = x; } return a; },this [j]] = [this [j],this [i]]; } 返回此; } });
此实现允许使用arr.shuffle()语法将数组改组。[&& &&&&华
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3