Last active
September 23, 2015 22:08
-
-
Save nenjiru/623461 to your computer and use it in GitHub Desktop.
重複しない乱数
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
/** | |
* Random numbers that do not overlap. | |
* @param {Number} length 生成数 | |
* @param {Number} max 閾値 | |
* @return {Array} | |
*/ | |
function uniqueRandom(length, max) | |
{ | |
var random, result = []; | |
while (result.length < length) | |
{ | |
random = Math.floor(Math.random() * max); | |
if (result.indexOf(random) < 0) result.push(random); | |
} | |
return result; | |
} | |
/* | |
http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/ | |
*/ | |
if (!Array.indexOf) | |
{ | |
Array.prototype.indexOf = function(v) | |
{ | |
for (var i in this) | |
{ | |
if (this[i] == v) return i; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jQuery依存を解消 (jQuery.inArray => Array.indexOf)