Skip to content

[js] 第24天 如何快速让一个数组乱序,写出来 #84

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第24天 如何快速让一个数组乱序,写出来

Activity

AnsonZnl

AnsonZnl commented on May 10, 2019

@AnsonZnl
Contributor

直接使用原生的sort()方法:

var arr= [1,2,3,4,5]
arr.sort(function(a,b){return Math.random() > 0.5 ? 1 : -1})
console.log(arr);// [ 1, 3, 5, 2, 4 ]
Mojitooooooooo

Mojitooooooooo commented on May 10, 2019

@Mojitooooooooo

使用array.sort()进行乱序存在一定问题,增大样本进行实验之后可以发现这种乱序方案并不是完全随机的(所有元素会大概率停留在自己的初始位置)(v8处理排序是小于10个是插入排序,大于10个是快排,排序算法复杂度介于O(n)与O(n2)之间,也就是存在两个元素都没有比较的机会,因此不是完全随机),这里可以使用Fisher–Yates shuffle(洗牌算法)
Array.prototype.shuffle = function() { var input = this; for (var i = input.length-1; i >=0; i--) { var randomIndex = Math.floor(Math.random()*(i+1)); var itemAtIndex = input[randomIndex]; input[randomIndex] = input[i]; input[i] = itemAtIndex; } return input; } var tempArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] tempArray.shuffle(); console.log(tempArray);

tiezhu92

tiezhu92 commented on May 10, 2019

@tiezhu92

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const map = new Map();
arr = arr.map(v => {
const random = Math.random();
map.set(random, v);
return random
}) //arr 替换成随机数, 原数组成员存入map
.sort((a, b) => a - b) //排序随机数
.map(v => map.get(v)); //取出数组成员填入数组

console.log(arr);

naokimidori

naokimidori commented on May 10, 2019

@naokimidori

arr.sort(function(a,b){ return Math.random()>.5 ? -1 : 1;});

changed the title [-][js] 第25天 如何快速让一个数组乱序,写出来[/-] [+][js] 第24天 如何快速让一个数组乱序,写出来[/+] on May 10, 2019
qqdnnd

qqdnnd commented on May 17, 2019

@qqdnnd
let arr= [1,2,3,4,5,6,7,8,9,10];
arr.map((item,index)=>{
    let random =Math.floor(Math.random() * arr.length);
    [arr[index],arr[random]] = [arr[random],arr[index]];
});
console.log(arr);
tzjoke

tzjoke commented on May 28, 2019

@tzjoke

为什么楼上要用三元运算符,直接这样不就完事了。。

arr.sort((a, b) => Math.random() - .5)

不过我们team随机算法也用的洗牌算法,思路就是从后往前遍历,然后随机(0, i+1),交换

function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1))
    [array[i], array[j]] = [array[j], array[i]]
  }
}
myprelude

myprelude commented on Jun 13, 2019

@myprelude
function mixi(arr){
  arr.sort(function(a,b){
   (Math,random()>0.5)?a-b>0:a-b<0
 })
}
shufangyi

shufangyi commented on Jul 22, 2019

@shufangyi
const swap = (arr, index1, index2) => {
  ;[arr[index1], arr[index2]] = [arr[index2], arr[index1]]
}

export default function shuffle(arr = []) {
  if (!Array.isArray(arr)) return
  let index = arr.length - 1
  for (; index > 0; index++) {
    const randomIndex = Math.floor(Math.random() * (index + 1))
    swap(arr, index, randomIndex)
  }
}
klren0312

klren0312 commented on Jul 23, 2019

@klren0312

前面有些, 为啥发之前不先测试下...
加个判断吧.

function shuffle (arr) {
  for (let i = 0, len = arr.length; i < len; i++) {
    let j = Math.floor(Math.random() * len)
    if (i !== j) [arr[i], arr[j]] = [arr[j], arr[i]]
  }
  return arr
}

console.log(shuffle([1,2,3,4,5,6,7,8,9,0]))

image

Vi-jay

Vi-jay commented on Jul 29, 2019

@Vi-jay
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
seho-dev

seho-dev commented on Aug 11, 2019

@seho-dev

//洗牌数组
export function shuffle(arr) {
const _arr = arr.slice();
for (let i = 0; i < _arr.length; i++) {
//调用
let j = getRandomNumber(0, i);
let t = _arr[i];
_arr[i] = _arr[j];
_arr[j] = t;
}
return _arr;
}

//随机取0-1中间值(包括0和1)
export function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1));
}

zivenday

zivenday commented on Aug 26, 2019

@zivenday

使用array.sort()进行乱序存在一定问题,增大样本进行实验之后可以发现这种乱序方案并不是完全随机的(所有元素会大概率停留在自己的初始位置)(v8处理排序是小于10个是插入排序,大于10个是快排,排序算法复杂度介于O(n)与O(n2)之间,也就是存在两个元素都没有比较的机会,因此不是完全随机),这里可以使用Fisher–Yates shuffle(洗牌算法)
Array.prototype.shuffle = function() { var input = this; for (var i = input.length-1; i >=0; i--) { var randomIndex = Math.floor(Math.random()*(i+1)); var itemAtIndex = input[randomIndex]; input[randomIndex] = input[i]; input[i] = itemAtIndex; } return input; } var tempArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] tempArray.shuffle(); console.log(tempArray);

大神,想问下,arr.sort(function(a,b){return Math.random() > 0.5 ? 1 : -1}),这种呢随机不是和Math.random的值密切相关吗,和sort有关系么?我的理解是sort是按照Math.random来排序,Math.random是完全随机的吧,那么这个整体应该也是完全随机的吧

23 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@haizhilin2013@zivenday@zebratt@never123450

        Issue actions

          [js] 第24天 如何快速让一个数组乱序,写出来 · Issue #84 · haizlin/fe-interview