Skip to content

Instantly share code, notes, and snippets.

@buyoh
Last active October 6, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buyoh/a12e3c3a1501223b03179b27691e0932 to your computer and use it in GitHub Desktop.
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
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