Skip to content

Instantly share code, notes, and snippets.

@nenjiru
Last active September 23, 2015 22:08
Show Gist options
  • Save nenjiru/623461 to your computer and use it in GitHub Desktop.
Save nenjiru/623461 to your computer and use it in GitHub Desktop.
重複しない乱数
/**
* 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;
}
}
@nenjiru
Copy link
Author

nenjiru commented Oct 15, 2010

jQuery依存を解消 (jQuery.inArray => Array.indexOf)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment