Shuffle an array using the Pencil-and-paper method I made it 12 bytes smaller by using a for/in loop
-
-
Save williammalo/2158116 to your computer and use it in GitHub Desktop.
Fisher–Yates shuffle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
a, // an array | |
b // placeholder | |
){ | |
for(b in a)a.push(a.splice(Math.random()*b,1)) | |
//take out a random piece of the array and append it to the end. | |
//this works because the splice method returns the piece it removes. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b){for(b=a.length;b;)a.push(a.splice(Math.random()*b--,1)[0])} | |
function(a,b){for(b in a)a.push(a.splice(Math.random()*b,1));return a} | |
function(a,b){for(b in a)a.push(a.splice(Math.random()*b,1))} | |
function(a){a.sort(function(){return .5-Math.random()})} | |
//legend: | |
//1. the original | |
//2. the fork with return (the original function doesn't return a, I think it should) | |
//3. the fork without return (less practical, but it's smaller) | |
//4. other method that is slightly smaller (not the same method as the original) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>Random numbers</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
// write a small example that shows off the API for your example | |
// and tests it in one fell swoop. | |
var myFunction = function(a,b){for(b in a)a.push(a.splice(Math.random()*b,1))} | |
var a = [1,2,3,4,5,6,7,8,9]; | |
myFunction(a); | |
document.getElementById( "ret" ).innerHTML = a | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment