Ruby quickies and useful idioms

This is a perennial favorite :slight_smile: I still pull in my home-made
version fairly often.

wow, I learned a new word. perennial. Had to look it up in the dictionary,
but it sounds good :wink:

From Merriam-Webster:
Etymology: Latin perennis, from per- throughout + annus year

ยทยทยท

--
Brian Schrรถder
http://www.brian-schroeder.de/

It is: biased in favour of suggestions I receive :slight_smile: The only major
suggestion I knocked back was a C implementation.

Gavin

ยทยทยท

On Monday, July 12, 2004, 9:19:01 AM, Mikael wrote:

You may even found extensions.rubyforge.org interesting

It's very nifty indeed. I heard it was biased in some obscure way,
though.

Mikael Brockman wrote:

gabriele renzi <surrender_it@rc1.vip.ukl.yahoo.com> writes:

[some shuffling stuff]

the best shuffle I can think of is the Grossian shuffle (quoting
Flroain Gross (actually written with a different character than ss):

It's actually "Florian GroรŸ", but I've seen the last character come out as an y with two dots above it which is why I usually use "Gross" where I can't be sure about it coming out right. :wink:

Oh, and thanks for naming this after me. :slight_smile:

class Array
  def shuffle
    sort_by {rand}
  end
end
You may even found extensions.rubyforge.org interesting

It's very nifty indeed. I heard it was biased in some obscure way,
though.

It's biased when rand returns the same float for two or more elements of the Array.

This means that it will be ~0.012% biased on an Array that has one million entries and ~0.0001% biased on an Array with 100000 entries when you're on a 32-bit machine.

On 64-bit machines you will have a 50% chance of one element being biased for an Array with 5,000,000,000 elements.

If that's still to likely for you you can use sort_by { Array.new(100) { rand }} instead.

The problem with all this is that people frequently confuse sort_by { rand } with sort { rand } and variants of it.

Oh, and it might even make sense to add it to Enumerable instead of Arrays. After all #sort_by is defined for all Enumerables.

Regards,
Florian Gross

Martin DeMello wrote:

A flatten_once would really be a useful method to have around.

martin

Yes. Maybe even flatten(n).

Common in gardening- annuals (you have to plant them each year) and perrenials (they come back- good for lazier gardeners). Unfortunately my wife didn't realize that perrenials still needed water, or they turn into sub-annuals :).

Also, "The Perennial Philosophy" by Aldous Huxley (of "Brave New World" novel fame), which was very influential on the hippie and new age movement, and the general trend for finding common religous grounds. A reference to this book was what sent me to the dictionary a couple of years back :).

Not so influential maybe as his "The Doors of Perception", which was about his drug taking experience in the 50s and a huge influence on the drug culture of the 60s. That title was also the source of the name of the band "The Doors".

Maybe in a few years Ruby will be know as the "perennial scripting language" :slight_smile:

Nick

Brian Schroeder wrote:

ยทยทยท

This is a perennial favorite :slight_smile: I still pull in my home-made
version fairly often.
   
wow, I learned a new word. perennial. Had to look it up in the dictionary,
but it sounds good :wink:

From Merriam-Webster:

Etymology: Latin perennis, from per- throughout + annus year

Gavin Sinclair <gsinclair@soyabean.com.au> writes:

ยทยทยท

On Monday, July 12, 2004, 9:19:01 AM, Mikael wrote:

You may even found extensions.rubyforge.org interesting

It's very nifty indeed. I heard it was biased in some obscure way,
though.

It is: biased in favour of suggestions I receive :slight_smile: The only major
suggestion I knocked back was a C implementation.

Haha, I was referring to sort_by { rand }.

  mikael

(quoting

Flroain Gross (actually written with a different character than ss):

It's actually "Florian GroรŸ", but I've seen the last character come out
as an y with two dots above it which is why I usually use "Gross" where
I can't be sure about it coming out right. :wink:

well, I see it fine (well, I think that is the german letter similar
to a beta that gets read kind of 'ss' )

Oh, and thanks for naming this after me. :slight_smile:

eh, I always wanted to name things :slight_smile:

It's very nifty indeed. I heard it was biased in some obscure way,
though.

It's biased when rand returns the same float for two or more elements of
the Array.

I don't understand: what happens if it returns the same float for two
different numbers? they just get near in the sorting, what's the
problem with that?

ยทยทยท

il Mon, 12 Jul 2004 02:59:35 +0200, Florian Gross <flgr@ccan.de> ha scritto::

Hi --

Martin DeMello wrote:
> A flatten_once would really be a useful method to have around.
>
> martin

Yes. Maybe even flatten(n).

There's flattenx on RAA (http://raa.ruby-lang.org/project/flattenx\).
It gives you four methods:

     Array#flatten_once # [[1,2,[3]]] => [1,2,[3]]
     Array#flatten_by(n) # does the above n times
     Array#flatten_once! # in-place version
     Array#flatten_by!(n) # in-place version

David

ยทยทยท

On Wed, 14 Jul 2004, Joel VanderWerf wrote:

--
David A. Black
dblack@wobblini.net

Oh! Ahem ... (cough cough) ... so was I ... (mutter mutter blush) :slight_smile:

Gavin

ยทยทยท

On Monday, July 12, 2004, 9:41:11 AM, Mikael wrote:

Gavin Sinclair <gsinclair@soyabean.com.au> writes:

On Monday, July 12, 2004, 9:19:01 AM, Mikael wrote:

You may even found extensions.rubyforge.org interesting

It's very nifty indeed. I heard it was biased in some obscure way,
though.

It is: biased in favour of suggestions I receive :slight_smile: The only major
suggestion I knocked back was a C implementation.

Haha, I was referring to sort_by { rand }.

=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

When rand returns the same float twice for 2 numbers, they will keep
their relative ordering in the end result. The Mersenne Twister Ruby
uses has got a period of 2^19937-1, but Kernel.rand only returns 64
bits, so collisions are way more likely than it would seem at first
sight. For instance, for an array with 10 million elements, the
probability of being slightly biased is a bit over 2.71*10^-6. If you
use 128 bits (by doing sort_by{[rand,rand]}), the probability decreases
to 1.47*10^-25 (at that point it is more likely that your computer
will explode or just crash).

For most intents and purposes, sort_by{rand} is unbiased enough.

ยทยทยท

On Mon, Jul 12, 2004 at 05:02:28PM +0900, gabriele renzi wrote:

I don't understand: what happens if it returns the same float for two
different numbers? they just get near in the sorting, what's the
problem with that?

(1..10).sort_by{1}

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux
over the wire". Film at 11.'
  -- Linus Torvalds