CopyPastor

Detecting plagiarism made easy.

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

Possible Plagiarism

Plagiarized on 2021-04-16
by TKoL

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;

I think one idea is to make a new array with only the randomizable things, shuffle it, and then place them back into the old array. Check this out:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var array = [ { randomize: true, value: 'value_1' }, { randomize: false, value: 'value_2' }, { randomize: false, value: 'value_3' }, { randomize: true, value: 'value_4' }, { randomize: false, value: 'value_5' }, { randomize: true, value: 'value_6' }, { randomize: true, value: 'value_7' }, ];
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array 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; }
// make a list of all randomizable elements var randomizable = array.filter(o => o.randomize); // shuffle their order shuffle(randomizable); // to make this easy to reason about, i think i'll make all the randomizeable elements in the first array null array.forEach(function(el, index) { if (el.randomize) array[index] = null; });
console.log(array);
// now, we can iterate over the null slots and place in elements from randomizable array.forEach(function(el, index) { if (el === null) { array[index] = randomizable.pop(); } });
console.log(array);
<!-- 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]; 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;