Monday, March 24, 2008

Generating unique random numbers

Interesting techniques:
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/fd64775134bd67df/dfff415085115cf0

Best one:
irb(main):001:0> (1..13).sort_by{rand}[0..7]
=> [7, 3, 8, 2, 11, 4, 1, 9]

Another one:

ar = []
while ar.length < 8
ar << rand(12) + 1
ar.uniq!
end

No comments: