<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: A random selection algorithm</title>
	<atom:link href="http://mcherm.com/permalinks/1/a-random-selection-algorithm/feed" rel="self" type="application/rss+xml" />
	<link>http://mcherm.com/permalinks/1/a-random-selection-algorithm</link>
	<description>Adventures in Programming</description>
	<lastBuildDate>Fri, 02 Apr 2010 16:07:43 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jeff</title>
		<link>http://mcherm.com/permalinks/1/a-random-selection-algorithm/comment-page-1#comment-5251</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Mon, 08 Dec 2008 21:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/?p=79#comment-5251</guid>
		<description>I agree with Shawn. This looks like, here is something wrong with the position variable. But items[available-1] does not fit too, because then you would pick the same element twice if your random condition is true twice in a row:
if( random() &lt; needed / available )

So you would need something like this:
items[available-k+needed]

But way of selection is not uniform distributed: the probability would increase to the end of the while-loop, so the elements at the end have allways better chance to get into the array than the first element.
 
Also this approach has a very simple problem: the items are only picked randomly, but the order stays the same. I mean, if i use this to take 3 elements from:
1 4 5 8 9
i could get:
5 4 1 
or 
9 5 1
but never: 1 9 5

I would suggest to generate a random number for every element from you list, and the sort the array by this number. After that you can just take the first k elements from your list. If you use round(256*rand()) as your random number (or any other constant number * rand()) and radixsort as your sortalgorithm, you can arrange this in linear time, and it stays deterministic. 

Of course, it is not that easy to implement as your snippet, so i would use my suggestion only if you really need an uniform distributed random numbers array. (=you work for a lottery) ;)</description>
		<content:encoded><![CDATA[<p>I agree with Shawn. This looks like, here is something wrong with the position variable. But items[available-1] does not fit too, because then you would pick the same element twice if your random condition is true twice in a row:<br />
if( random() &lt; needed / available )</p>
<p>So you would need something like this:<br />
items[available-k+needed]</p>
<p>But way of selection is not uniform distributed: the probability would increase to the end of the while-loop, so the elements at the end have allways better chance to get into the array than the first element.</p>
<p>Also this approach has a very simple problem: the items are only picked randomly, but the order stays the same. I mean, if i use this to take 3 elements from:<br />
1 4 5 8 9<br />
i could get:<br />
5 4 1<br />
or<br />
9 5 1<br />
but never: 1 9 5</p>
<p>I would suggest to generate a random number for every element from you list, and the sort the array by this number. After that you can just take the first k elements from your list. If you use round(256*rand()) as your random number (or any other constant number * rand()) and radixsort as your sortalgorithm, you can arrange this in linear time, and it stays deterministic. </p>
<p>Of course, it is not that easy to implement as your snippet, so i would use my suggestion only if you really need an uniform distributed random numbers array. (=you work for a lottery) ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shawn Poulson</title>
		<link>http://mcherm.com/permalinks/1/a-random-selection-algorithm/comment-page-1#comment-228</link>
		<dc:creator>Shawn Poulson</dc:creator>
		<pubDate>Sat, 28 Jun 2008 16:15:30 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/?p=79#comment-228</guid>
		<description>Hmm, it looks like you&#039;re not incrementing position.  This code will retrieve the first item in each iteration.

I&#039;d recommend replacing items[position] with items[available-1] and ditch position altogether.

Very good code snippet.  I think I&#039;ll keep it.</description>
		<content:encoded><![CDATA[<p>Hmm, it looks like you&#8217;re not incrementing position.  This code will retrieve the first item in each iteration.</p>
<p>I&#8217;d recommend replacing items[position] with items[available-1] and ditch position altogether.</p>
<p>Very good code snippet.  I think I&#8217;ll keep it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

