Shuffle an array using the Pencil-and-paper method
-
-
Save tsaniel/1345090 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=a.length ;b ;) | |
a.push(a.splice(Math.random()*b--,1)[0]) // randomly move an element to the end | |
} |
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){with(a)for(a=length;a;)push(splice(Math.random()*a--,1)[0])} // with version and save 1 byte thanks to @subzey |
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
{ | |
"name": "shuffle", | |
"description": "Shuffle an array.", | |
"keywords": [ | |
"Fisher–Yates", | |
"shuffle", | |
"array" | |
] | |
} |
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=a.length;b;)a.push(a.splice(Math.random()*b--,1)[0])} | |
var a = [1,2,3,4,5,6,7,8,9]; | |
myFunction(a); | |
document.getElementById( "ret" ).innerHTML = a | |
</script> |
@tsaniel: "for in" loops also work with objects, for example: {foo:bar,lorem:ipsum} if thats whats bothering you.
If you don't change your own function, I'll make a fork =D
I think an object does not have methods "push" and "splice"...
Anyway, feel free to fork the code :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@williammalo: Thanks for you advice!
But I am afraid that there will be an error if you input a non-array object.