5

Possible Duplicate:
How do I generate a list of n unique random numbers in Ruby?

I want to do:

Random.rand(0..10).times do
  puts Random.rand(0..10)
end

with the exception that if a random number has already been displayed it cannot be displayed again. How do I do this most easily?

Community
  • 1
  • 1
William Stocks
  • 341
  • 1
  • 3
  • 14

2 Answers2

8

As a quick one-liner:

rands = (0..10).to_a.shuffle[0, Random.rand(0..10)]
Chowlett
  • 45,935
  • 20
  • 116
  • 150
4

If you are using Ruby 1.9 another one-liner with Array#sample

rands = (0..10).to_a.sample(­Random.ran­­d(0..10))
nemesv
  • 138,284
  • 16
  • 416
  • 359