Created
June 20, 2013 21:06
-
-
Save kazua/5826649 to your computer and use it in GitHub Desktop.
Arrayのpop()のランダム版
This file contains hidden or 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
| //write kazua | |
| if (!Array.prototype.randompop) { | |
| Array.prototype.randompop = function() { | |
| "use strict"; | |
| if (this == null) | |
| throw new TypeError(); | |
| var t = Object(this), len = t.length >>> 0; | |
| if (len == 0) | |
| return null; | |
| var r = Math.floor(Math.random() * len); | |
| var n = t[r]; | |
| t = t.filter(function(value, index, array) { | |
| if (index != r) | |
| return true; | |
| }); | |
| this.length = 0; | |
| Array.prototype.push.apply(this, t); | |
| return n; | |
| }; | |
| } | |
| var a = new Array(1, 3, 4, 6, 7, 8, 0, 5, 9, 0); | |
| function test() { | |
| var len = a.length; | |
| for (i = 0; i < len; i++) { | |
| var n = a.randompop(); | |
| alert("popした要素" + n); | |
| alert("現配列" + a); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment