Hướng dẫn javascript shuffle list - danh sách xáo trộn javascript

Shuffle mảng tại chỗ

Nội dung chính ShowShow

  • Các giải pháp khác
  • Tùy chỉnh sắp xếp
  • Thuật toán Fisher-Yates
  • Làm thế nào để bạn xáo trộn trong JavaScript?
  • Làm thế nào để bạn xáo trộn các đối tượng trong một mảng?
  • Làm thế nào để bạn ngẫu nhiên một danh sách trong java?
  • Chức năng xáo trộn là gì?

function shuffleArr (array){
    for (var i = array.length - 1; i > 0; i--) {
        var rand = Math.floor(Math.random() * (i + 1));
        [array[i], array[rand]] = [array[rand], array[i]]
    }
}

Es6 tinh khiết, lặp đi lặp lại

const getShuffledArr = arr => {
    const newArr = arr.slice()
    for (let i = newArr.length - 1; i > 0; i--) {
        const rand = Math.floor(Math.random() * (i + 1));
        [newArr[i], newArr[rand]] = [newArr[rand], newArr[i]];
    }
    return newArr
};

Kiểm tra độ tin cậy và hiệu suất

Một số giải pháp trên trang này không đáng tin cậy (chúng chỉ ngẫu nhiên một phần cho mảng). Các giải pháp khác ít hiệu quả hơn đáng kể. Với testShuffleArrayFun (xem bên dưới), chúng tôi có thể kiểm tra các chức năng xáo trộn cho độ tin cậy và hiệu suất.

function testShuffleArrayFun(getShuffledArrayFun){
    const arr = [0,1,2,3,4,5,6,7,8,9]

    var countArr = arr.map(el=>{
        return arr.map(
            el=> 0
        )
    }) //   For each possible position in the shuffledArr and for 
       //   each possible value, we'll create a counter. 
    const t0 = performance.now()
    const n = 1000000
    for (var i=0 ; i{countArr[key][value]++}
        )
    }
    const t1 = performance.now()
    console.log(`Count Values in position`)
    console.table(countArr)

    const frequencyArr = countArr.map( positionArr => (
        positionArr.map(  
            count => count/n
        )
    )) 

    console.log("Frequency of value in position")
    console.table(frequencyArr)
    console.log(`total time: ${t1-t0}`)
}

Các giải pháp khác

Tùy chỉnh sắp xếp

Thuật toán Fisher-Yates

const getShuffledArr = arr => {
    if (arr.length === 1) {return arr};
    const rand = Math.floor(Math.random() * arr.length);
    return [arr[rand], ...getShuffledArr(arr.filter((_, i) => i != rand))];
};

Làm thế nào để bạn xáo trộn trong JavaScript?

function getShuffledArr (arr){
    return [...arr].map( (_, i, arrCopy) => {
        var rand = i + ( Math.floor( Math.random() * (arrCopy.length - i) ) );
        [arrCopy[rand], arrCopy[i]] = [arrCopy[i], arrCopy[rand]]
        return arrCopy[i]
    })
}

Làm thế nào để bạn xáo trộn các đối tượng trong một mảng?

function getShuffledArr (arr){
    return arr.reduce( 
        (newArr, _, i) => {
            var rand = i + ( Math.floor( Math.random() * (newArr.length - i) ) );
            [newArr[rand], newArr[i]] = [newArr[i], newArr[rand]]
            return newArr
        }, [...arr]
    )
}

Hướng dẫn javascript shuffle list - danh sách xáo trộn javascript

Làm thế nào để bạn ngẫu nhiên một danh sách trong java?

Tùy chỉnh sắp xếp

Thuật toán Fisher-Yates

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const shuffledArray = array.sort((a, b) => 0.5 - Math.random());

Làm thế nào để bạn xáo trộn trong JavaScript?

Làm thế nào để bạn xáo trộn các đối tượng trong một mảng?

Làm thế nào để bạn ngẫu nhiên một danh sách trong java?

Chức năng xáo trộn là gì?

Es6 tinh khiết, lặp đi lặp lại

Thuật toán Fisher-Yates

Làm thế nào để bạn xáo trộn trong JavaScript?

const shuffleArray = array => {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    const temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
}

Làm thế nào để bạn xáo trộn trong JavaScript?

Làm thế nào để bạn xáo trộn các đối tượng trong một mảng?

Làm thế nào để bạn ngẫu nhiên một danh sách trong java?

Làm thế nào để bạn xáo trộn trong JavaScript?

Làm thế nào để bạn xáo trộn các đối tượng trong một mảng?provide a custom function to a . sort() . const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const shuffledArray = array. sort((a, b) => 0.5 - Math.

Làm thế nào để bạn xáo trộn các đối tượng trong một mảng?

Làm thế nào để bạn ngẫu nhiên một danh sách trong java?iterate through the array elements in a for loop. Then, we use the Random class to generate a random index number. Then swap the current index element with the randomly generated index element. At the end of the for loop, we will have a randomly shuffled array.

Làm thế nào để bạn ngẫu nhiên một danh sách trong java?

Chức năng xáo trộn là gì? to shuffle a list with the standard Java library or Collections. shuffle(Arrays. asList(a)) to shuffle the entries in an array .

Chức năng xáo trộn là gì?

Es6 tinh khiết, lặp đi lặp lạirandomizes the order of the elements in the array. This function assigns new keys for the elements in the array. Existing keys will be removed (See Example below).