Last active
October 6, 2016 14:45
-
-
Save buyoh/a12e3c3a1501223b03179b27691e0932 to your computer and use it in GitHub Desktop.
1からnまでの乱数をm個重複せずO(m)でランダムに選ぶrubyのプログラム。 choose m integers from 1 to n randomly
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
def rands(n,m) | |
result=[] | |
memo ={} | |
m = n if n < m | |
1.upto(m){|low| | |
v = rand(low..n) | |
if !memo[v] | |
result << v | |
memo[v] = memo[low] || low | |
else | |
result << memo[v] | |
memo[v] = memo[low] || low | |
end | |
} | |
return result | |
end | |
100.times{ | |
puts rands(10,5)*" " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment