Created
January 11, 2017 10:11
-
-
Save Yosuke-Kawakami/4d7051b8b99c830eaf4f422de6d8e616 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
function gen_random(num) | |
{ | |
var LIMIT_NUM = Math.pow(10, 2); | |
if(num > LIMIT_NUM) return new Array(); | |
var list = new Array(); | |
for(var i = 0; i<= num; i++) | |
{ | |
list.push(i); | |
} | |
var arr = new Array(); | |
while(1) | |
{ | |
var i = Math.floor(Math.random() * LIMIT_NUM) % num; | |
var index = list.indexOf(i); | |
if(index > -1) | |
{ | |
list[index] = -1; | |
arr.push(i); | |
if(arr.length >= num) break; | |
} | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment