Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created November 7, 2011 14:03
Show Gist options
  • Save tsaniel/1345090 to your computer and use it in GitHub Desktop.
Save tsaniel/1345090 to your computer and use it in GitHub Desktop.
Fisher–Yates shuffle
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
}
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
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.
{
"name": "shuffle",
"description": "Shuffle an array.",
"keywords": [
"Fisher–Yates",
"shuffle",
"array"
]
}
<!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
Copy link
Author

tsaniel commented Nov 8, 2011

Thanks, the with trick is awesome!
However, I think it's not worth it.
Actually, .sort(function(){return.5-Math.random()}) can be the shortest code but inefficient.

@vjeux
Copy link

vjeux commented Dec 12, 2011

It looks like you don't need to cast the first argument of splice into an int, the function already does it.

[1, 2, 3].splice(1, 1)
> [2]
[1, 2, 3].splice(1.2, 1)
> [2]

I tested it on ie9, chrome, firefox and opera. Not sure if it works on all versions but I suppose it would

@tsaniel
Copy link
Author

tsaniel commented Dec 12, 2011

ES3 15.4.4.12 says it would.

4. Call ToInteger(start).

@vjeux: thanks for your great work!

@williammalo
Copy link

you can make it shorter

function(a,b){for(b in a)a.push(a.splice(Math.random()*b,1))}

@tsaniel
Copy link
Author

tsaniel commented Mar 21, 2012

@williammalo: Thanks for you advice!
But I am afraid that there will be an error if you input a non-array object.

@williammalo
Copy link

@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

@tsaniel
Copy link
Author

tsaniel commented Mar 22, 2012

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