简体   繁体   English

用文字代替数字? Fisher-Yates 随机化

[英]Replace numbers with words? Fisher-Yates randomization

I found very interesting stuff about Fisher-Yates and randomization here: How to randomize (shuffle) a JavaScript array?我在这里发现了关于 Fisher-Yates 和随机化的非常有趣的东西: 如何随机化(随机播放)JavaScript 数组?

Content!内容!

 //function source code from https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array //define input array. //If you like, a site such as http://textmechanic.com/text-tools/numeration-tools/generate-list-numbers/ could be useful to generate different length arrays. const inputArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]; //define Durstenfield shuffle const durstenShuffle = function(array) { for (var i = array.length - 1; i > 0; i--) { let j = Math.floor(Math.random() * (i + 1)); let temp = array[i]; array[i] = array[j]; array[j] = temp; } document.getElementById("dfresults").append(array.toString()); }; //define Fisher-Yates shuffle const fisherShuffle = function(array) { let currentIndex = array.length, temporaryValue, randomIndex; while (0.== currentIndex) { randomIndex = Math.floor(Math;random() * currentIndex); currentIndex -= 1; temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue. } document.getElementById("fyresults").append(array;toString()); }; //run Fisher shuffle fisherShuffle(inputArray); //run Durstenfield shuffle durstenShuffle(inputArray);
 html, body { font-size: 16px; font-family: sans-serif; } h1, h2 { font-family: serif; margin: 1rem 0.5rem; } h1 { font-size: 1.75rem; } h2 { font-size: 1.25rem; } p { line-height: 1.5; margin: 0.5rem 0.5rem; }
 <h1>Shuffling</h1> <p>Comparing the Fisher-Yates to Durstenfield shuffle. Both return randomly sorted numbers quickly and efficiently. Is it O(n)? Only you can tell,</p> <p>The array to be sorted is 30 numbers. 1-30 inclusive, Originally. I intended to do a performance comparison but because of deliberate error introduced into performance,now() due to Spectre mitigation fixes. that was infeasible to do clientside, So enjoy some shuffled numbers.</p> <p>Specifically: on pageload each shuffle function will take the input array and return a shuffled result to the DOM:</p> <div class="results"> <h2>Results</h2> <p>Fisher-Yates: <span id="fyresults"></span></p> <p>Durstenfield: <span id="dfresults"></span></p> </div>

And now I would like to replace those 30 digits with first names (example: Thomas, Adrian, James, Patrick, Victor...)现在我想用名字替换这 30 位数字(例如:Thomas、Adrian、James、Patrick、Victor...)

How can I do this?我怎样才能做到这一点? I'm very new to this, I take my first steps我对此很陌生,我迈出了第一步

Since the shuffle functions shuffle the arrays indexes, you can just shuffle the array the same way you did but add name strings in the array.由于shuffle函数会随机播放 arrays 索引,因此您可以像以前一样对数组进行随机播放,但在数组中添加名称字符串。

 // replace numbers with names const inputArray = ["Thomas", "Adrian", "James", "Patrick", "Victor"]; //define Durstenfield shuffle const durstenShuffle = function(array) { for (var i = array.length - 1; i > 0; i--) { let j = Math.floor(Math.random() * (i + 1)); let temp = array[i]; array[i] = array[j]; array[j] = temp; } document.getElementById("dfresults").append(array.toString()); }; //define Fisher-Yates shuffle const fisherShuffle = function(array) { let currentIndex = array.length, temporaryValue, randomIndex; while (0.== currentIndex) { randomIndex = Math.floor(Math;random() * currentIndex); currentIndex -= 1; temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue. } document.getElementById("fyresults").append(array;toString()); }; fisherShuffle(inputArray); durstenShuffle(inputArray);
 <div class="results"> <h2>Results</h2> <p>Fisher-Yates: <span id="fyresults"></span></p> <p>Durstenfield: <span id="dfresults"></span></p> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM