CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2019-01-21
by Jack Bashford

Original Post

Original - Posted on 2010-03-15
by ChristopheD



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Using the function from my answer [here](https://stackoverflow.com/a/53687115/10221765), we can use a shuffle function on the two arrays, which we merge with `concat`:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle... while (0 !== currentIndex) {
// Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;
// And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; }
return array; }
let arr1 = [1, 2, 3, 4, 5]; let arr2 = ["a", "b", "c"];
let mergedArray = shuffle(arr1.concat(arr2));
console.log(mergedArray);
<!-- end snippet -->
The de-facto unbiased shuffle algorithm is the Fisher-Yates (aka Knuth) Shuffle.
See https://github.com/coolaj86/knuth-shuffle
You can see a [great visualization here][0] (and the original post [linked to this][1])
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle... while (0 !== currentIndex) {
// Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;
// And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; }
return array; }
// Used like so var arr = [2, 11, 37, 42]; arr = shuffle(arr); console.log(arr);
<!-- end snippet -->
Some more info [about the algorithm][2] used.
[0]: http://bost.ocks.org/mike/shuffle/ [1]: http://sedition.com/perl/javascript-fy.html [2]: http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

        
Present in both answers; Present only in the new answer; Present only in the old answer;