Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array#choice always produce the same sequence

Reply
Thread Tools

Array#choice always produce the same sequence

 
 
Stefano Crocco
Guest
Posts: n/a
 
      02-05-2009
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


 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      02-05-2009
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

 
Reply With Quote
 
 
 
 
ruud grosmann
Guest
Posts: n/a
 
      02-05-2009
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 <> 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
>
>


 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      02-05-2009
Alle Thursday 05 February 2009, ruud grosmann 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 =3D [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 lik=

e:
> >
> > ruby -e 'a =3D [1, 2, 3, 4]; p 10.times.map{a.choice}.join'
> >
> > always give the same result, while
> >
> > ruby -e 'a =3D [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


>Hi Stefano,
>
>it seems to work for me:
>
>irb(main):003:0> a =3D [1, 2, 3, 4]
>=3D> [1, 2, 3, 4]
>irb(main):004:0> p 10.times.map{a.choice}.join
>"4414312344"
>=3D> nil
>irb(main):006:0> p 10.times.map{a[rand(a.size)]}.join
>"4342411331"
>=3D> nil
>irb(main):007:0> =C2=A0p 10.times.map{a.choice}.join
>"2333341212"
>=3D> nil
>irb(main):008:0> p 10.times.map{a.choice}.join
>"1312441122"
>=3D> nil
>irb(main):009:0> =C2=A0p 10.times.map{a.choice}.join
>"2412444223"
>=3D> nil
>Z ruby -v
>ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]


Maybe I wasn't clear in explaining the issue. Within a single irb session (=
or=20
ruby invocation in general) I, too, get different results at each invocatio=
n.=20
The problem is that if I run the script twice, I get the same results for e=
ach=20
call to choice (that is, the first call to choice gives the same result at=
=20
each ruby invocation; the second call always gives the same result, which c=
an=20
be different from the first, and so on). A simpler example:

=46ile: test.rb

p [1,2,3,4].choice

ruby test.rb
=3D> 4
ruby test.rb
=3D> 4
ruby test.rb
=3D> 4

And so on. If at the beginning of the script I add a call to Kernel#srand=20
(without arguments), I get the correct behaviour, that is a different numbe=
r=20
every time the script is run.

Stefano

 
Reply With Quote
 
Radosław Bułat
Guest
Posts: n/a
 
      02-05-2009
On Thu, Feb 5, 2009 at 2:42 PM, Stefano Crocco <> wr=
ote:
> p [1,2,3,4].choice
>
> ruby test.rb
> =3D> 4
> ruby test.rb
> =3D> 4
> ruby test.rb
> =3D> 4
>
> And so on. If at the beginning of the script I add a call to Kernel#srand
> (without arguments), I get the correct behaviour, that is a different num=

ber
> every time the script is run.



It looks like it was fixed in ruby1.9 (notice that #choice has been
renamed to #sample):

ruby1.9.1 -e "p [1,2,3,4].sample"
4
ruby1.9.1 -e "p [1,2,3,4].sample"
1
ruby1.9.1 -e "p [1,2,3,4].sample"
2
ruby1.9.1 -e "p [1,2,3,4].sample"
3
ruby1.9.1 -e "p [1,2,3,4].sample"
1
ruby1.9.1 -e "p [1,2,3,4].sample"

File a bug or ask in ruby-core if it's desired behavior.

--=20
Pozdrawiam

Rados=B3aw Bu=B3at
http://radarek.jogger.pl - m=F3j blog

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to iterate over sequence and non-sequence ? stef mientki Python 13 10-20-2007 10:21 AM
can java produce .exe? if it can produce jar,how do you do? aungkopyay@gmail.com Java 5 10-27-2006 02:07 AM
XSLT transformaton (result XML shld be in same sequence always... ravi.velamuri@gmail.com XML 1 02-26-2006 07:57 PM
Same ASP page executed 2 ways produce different results...strange Benman ASP General 0 12-09-2005 06:47 PM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57