<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>We Heart Code &#187; arrays</title>
	<atom:link href="http://www.weheartcode.com/category/arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weheartcode.com</link>
	<description>A discourse on programming</description>
	<lastBuildDate>Sun, 17 Jan 2010 02:16:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Randomizing an array in Ruby</title>
		<link>http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/</link>
		<comments>http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/#comments</comments>
		<pubDate>Sat, 07 Oct 2006 03:49:34 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[arrays]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/</guid>
		<description><![CDATA[Ruby's Array class has no method to randomize the contents of an array.  Of course, Array can easily be extended to include this functionality.  And like anything else in Ruby, there's more than one way to do it.  Here's one way:


class Array
&#160; def randomize
&#160; &#160; arr=self.dup
&#160; &#160; self.collect &#123; arr.slice!&#40;rand&#40;arr.length&#41;&#41; &#125;
&#160; end
&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby's Array class has no method to randomize the contents of an array.  Of course, Array can easily be extended to include this functionality.  And like anything else in Ruby, there's more than one way to do it.  Here's one way:</p>
<div class="syntax_hilite">
<div id="ruby-2">
<div class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span></p>
<p>&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> randomize<br />
&nbsp; &nbsp; arr=<span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">dup</span><br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> arr.<span style="color:#9900CC;">slice</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span>arr.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></p>
<p>&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> randomize!<br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">replace</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">randomize</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></p>
<p>&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> random<br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">randomize</span>.<span style="color:#9900CC;">first</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></p>
<p><span style="color:#9966CC; font-weight:bold;">end</span></div>
</div>
</div>
<p></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/&amp;title=Randomizing+an+array+in+Ruby" title="Add 'Randomizing an array in Ruby' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Randomizing an array in Ruby' to Del.icio.us" alt="Add 'Randomizing an array in Ruby' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/&amp;title=Randomizing+an+array+in+Ruby" title="Add 'Randomizing an array in Ruby' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Randomizing an array in Ruby' to digg" alt="Add 'Randomizing an array in Ruby' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Randomizing+an+array+in+Ruby&amp;u=http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/" title="Add 'Randomizing an array in Ruby' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Randomizing an array in Ruby' to FURL" alt="Add 'Randomizing an array in Ruby' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/&amp;title=Randomizing+an+array+in+Ruby" title="Add 'Randomizing an array in Ruby' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Randomizing an array in Ruby' to reddit" alt="Add 'Randomizing an array in Ruby' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/" title="Add 'Randomizing an array in Ruby' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Randomizing an array in Ruby' to Technorati" alt="Add 'Randomizing an array in Ruby' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/&amp;title=Randomizing+an+array+in+Ruby" title="Add 'Randomizing an array in Ruby' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Randomizing an array in Ruby' to Google Bookmarks" alt="Add 'Randomizing an array in Ruby' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2006/10/06/randomizing-an-array-in-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
