From: Robert Klemme Date: 2010-10-22T15:29:12+09:00 Subject: Re: generating random argument lists On Thu, Oct 21, 2010 at 5:27 PM, Brian Candler wrote: > Robert Klemme wrote in post #956028: >>> That also gives between 0 and 9 arrays, each with between 0 and 4 >>> elements, each element being between 0 and 3. >> >> Note that rand(x) can return 0 so it would be more natural to start >> ranges at 0 IMHO. > > I did that originally, but (0..0).map { ... } gives a one-element array, > and I wanted to have empty arrays too. You can omit the far end with > (0...0), but then (0...rand(5)) gives between 0 and 3, which is even > more confusing IMO. No, (0...rand(5)).map... will create arrays with 0 to 4 elements. I find that totally natural irb(main):010:0> 5.times do |rnd| irb(main):011:1* printf "%2d %p\n",rnd,(0...rnd).map{"x"} irb(main):012:1> end 0 [] 1 ["x"] 2 ["x", "x"] 3 ["x", "x", "x"] 4 ["x", "x", "x", "x"] => 5 >> Another version with less brackets especially around ranges where I >> find them ugly: >> >> arrays = rand(10).times.map { rand(5).times.map { rand 4 } } > > I'm an old fogey and avoid avoid 1.8.7-isms :-) I really had to look up "fogey". Learn something new every day. :-) Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/