From: dblack@... Date: 2006-03-17T06:05:14+09:00 Subject: Re: Card tricks with Ruby Hi -- On Fri, 17 Mar 2006, grrr wrote: > So suppose you have a deck of cards, that might have some number of cards. > > First the cards are shuffled, in effect placed in random order. > > Then the deck is split, ie. some number of cards are lifted from the top > of the deck and placed under the remaining cards. > > How would one implement this? I was thinking of using an array, but how to > shuffle the deck, and how to split the deck? You could do: def shuffle sort_by { rand } end (Disclaimer: I haven't really followed the discussions of how random this and other techniques for shuffling are.) Here's a demo of how you might cut the deck (using a small deck here): irb(main):015:0> deck = *1..10 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] irb(main):016:0> deck.concat(deck.slice!(0,5)) => [6, 7, 8, 9, 10, 1, 2, 3, 4, 5] David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black