ruud grosmann
2/5/2009 12:25:00 PM
Hi Stefano,
it seems to work for me:
irb(main):003:0> a = [1, 2, 3, 4]
=> [1, 2, 3, 4]
irb(main):004:0> p 10.times.map{a.choice}.join
"4414312344"
=> nil
irb(main):006:0> p 10.times.map{a[rand(a.size)]}.join
"4342411331"
=> nil
irb(main):007:0> p 10.times.map{a.choice}.join
"2333341212"
=> nil
irb(main):008:0> p 10.times.map{a.choice}.join
"1312441122"
=> nil
irb(main):009:0> p 10.times.map{a.choice}.join
"2412444223"
=> nil
Z ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
On 05/02/2009, Stefano Crocco <stefano.crocco@alice.it> wrote:
> Alle Thursday 05 February 2009, Stefano Crocco ha scritto:
>> I've just stumbled upon a strange behaviour of Array#choice (using ruby
>> 1.8.7- p72). As far I understand, an_array.choice should be (almost) the
>> same as an_array[rand(an_array.size)]. Thus, the two following pieces of
>> code should be equivalent
>>
>> a = [1, 2, 3, 4]
>> p 10.times.map{a.choice}.join
>> p 10.times.map{a[rand(a.size)].join}
>>
>> However, this doesn't seem to be the case. In particular, something like:
>>
>> ruby -e 'a = [1, 2, 3, 4]; p 10.times.map{a.choice}.join'
>>
>> always give the same result, while
>
> Sorry, hit the "Send" button too soon. Here's the full version
>
> I've just stumbled upon a strange behaviour of Array#choice (using ruby
> 1.8.7- p72). As far I understand, an_array.choice should be (almost) the
> same as an_array[rand(an_array.size)]. Thus, the two following pieces of
> code should be equivalent
>
> a = [1, 2, 3, 4]
> p 10.times.map{a.choice}.join
> p 10.times.map{a[rand(a.size)]}.join
>
> However, this doesn't seem to be the case. In particular, something like:
>
> ruby -e 'a = [1, 2, 3, 4]; p 10.times.map{a.choice}.join'
>
> always give the same result, while
>
> ruby -e 'a = [1, 2, 3, 4]; p 10.times.map{a[rand(a.size)]}.join'
>
> gives a different result every time the command is executed (which, in my
> opinion, is the correct behaviour).
>
> It seems that to get the correct behaviour from choice, you need to call
> srand
> before using it. Does anyone know why this is necessary with choice but not
> with rand? Is it the intended behaviour or a bug?
>
> Thanks
>
> Stefano
>
>