重複しない乱数【メモ:関数】
投稿者:
fragile
投稿日:2013/06/07 18:35
テストさえしてないです。
ツイート
def RAND(n)#とりあえず自分が必要なもの
a=createArray()
i=0
while i < n
a[i] = i
i=i+1
end
i=0
while i < n
x = rand(n)
y = a[i]
a[i] = a[x]
a[x] = y
i=i+1
end
return a
end
############################################################
############################################################
def setRand(n)#0からn-1までの重複しない乱数を作成
if !getVariable("重複しない乱数")
setVariable("重複しない乱数", createArray())
if n
getVariable("重複しない乱数")[0] = n
else
getVariable("重複しない乱数")[0] = 10
end
else
if n
getVariable("重複しない乱数")[0] = n
end
end
i=0
while i < getVariable("重複しない乱数")[0]
getVariable("重複しない乱数")[i+1] = i
i=i+1
end
i=1
while i < getVariable("重複しない乱数")[0]+1
x = rand(getVariable("重複しない乱数")[0])+1
y = getVariable("重複しない乱数")[i]
getVariable("重複しない乱数")[i] = getVariable("重複しない乱数")[x]
getVariable("重複しない乱数")[x] = y
i=i+1
end
end
############################################################
def getRand()#setRandで作成した乱数を取得。 尽きた時は-1を返す
if 1 < getArrayLength(getVariable("重複しない乱数"))
return popArray(getVariable("重複しない乱数"))
else
return -1
end
end
############################################################
def checkRand(k)#まだ切り出せるかチェック kをtrueにすると残りの数が表示される
if k
return getArrayLength(getVariable("重複しない乱数"))-1
else
if 0 < (getArrayLength(getVariable("重複しない乱数"))-1)
return true
else
return false
end
end
endコメントする
コメントするには、ログインする必要があります。
コメント一覧
コメントはありません。