<div><div class="im" style="font-size: medium; "><div style="font-size: 10pt; "><span style="font-size: 14px; line-height: 16px; "><font class="Apple-style-span" face="verdana, sans-serif">> Shuffle [1..20], then take 5?</font></span></div>
<div style="font-size: 10pt; "><font class="Apple-style-span" face="verdana, sans-serif"><span class="Apple-style-span" style="font-size: 14px; line-height: 16px; ">> Yes, so</span><span class="Apple-style-span" style="font-size: 14px; line-height: 16px; "> simple, I'm embarrassed I didn't think of it.</span></font></div>
</div><div><span class="Apple-style-span" style="font-size: 14px; line-height: 16px;"><font class="Apple-style-span" face="verdana, sans-serif"><br></font></span></div></div><div><span class="Apple-style-span" style="font-size: 14px; line-height: 16px; "><font class="Apple-style-span" face="verdana, sans-serif">That works well for small numbers, but I'm guessing it will evaluate the entire list so it should not be used for large inputs. If you have a large interval and use a relatively small part of it, the following function should be significantly faster (it builds a random permutation lazily): </font></span></div>
<div><font class="Apple-style-span" face="verdana, sans-serif"><span class="Apple-style-span" style="font-size: 14px; line-height: 16px;"><br></span></font></div><div><font class="Apple-style-span" face="'courier new', monospace">import System.Random</font></div>
<div><div><font class="Apple-style-span" face="'courier new', monospace">randomOrder :: (Ord a, Num a, Random a, RandomGen g) => (a,a) -> g -> [a]</font></div><div><font class="Apple-style-span" face="'courier new', monospace">randomOrder (low,high) g </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> | low > high = []</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> | otherwise = let</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"> (a,g') = randomR (low,high) g</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> (gl,gr) = split g'</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> in a : mergeRandom (a-1-low,randomOrder (low,a-1) gl) </font></div><div><font class="Apple-style-span" face="'courier new', monospace"> (high-a-1, randomOrder (a+1,high) gr) g'</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> where</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> mergeRandom (_,[]) (_,xs) _ = xs</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> mergeRandom (_,xs) (_,[]) _ = xs</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> mergeRandom (lx,x:xs) (ly,y:ys) g = let</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> (pick,g') = randomR (1,lx + ly) g</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> in if pick <= lx </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> then x : mergeRandom (lx-1,xs) (ly,y:ys) g'</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> else y : mergeRandom (lx,x:xs) (ly-1,ys) g'</font></div>
</div><div><br></div><br><br><div class="gmail_quote">On 14 June 2011 04:31, michael rice <span dir="ltr"><<a href="mailto:nowgate@yahoo.com">nowgate@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="top" style="font:inherit"><div><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td style="font:inherit"><font size="2">Thanks, all.</font><div style="font-family:arial;font-size:10pt">
<br></div><div style="font-family:arial;font-size:10pt">It seemed like something like this should exist in a prob/stat package, and if so, didn't want to reinvent the wheel.</div><div class="im"><div style="font-family:arial;font-size:10pt">
<br></div><div style="font-family:arial;font-size:10pt"><span style="font-family:monospace;font-size:14px;line-height:16px">Shuffle [1..20], then take 5?</span></div><div style="font-family:arial;font-size:10pt"><span style="font-family:monospace;font-size:14px;line-height:16px"><br>
</span></div></div><div style="font-family:arial;font-size:10pt"><span style="font-family:monospace;font-size:14px;line-height:16px">Yes, so simple, I'm embarrassed I didn't think of it.</span></div><div style="font-family:arial;font-size:10pt">
<span style="font-family:monospace;font-size:14px;line-height:16px"><br></span></div><div style="font-family:arial;font-size:10pt"><span style="font-family:monospace;font-size:14px;line-height:16px">Michael</span></div><div style="font-family:arial;font-size:10pt">
<span style="font-family:monospace;font-size:14px;line-height:16px"><br></span></div><div><font face="monospace"><span style="font-size:14px;line-height:16px"><br></span></font></div><div style="font-family:arial;font-size:10pt">
<br>--- On <b>Mon, 6/13/11, Felipe Almeida Lessa <i><<a href="mailto:felipe.lessa@gmail.com" target="_blank">felipe.lessa@gmail.com</a>></i></b> wrote:<br><blockquote style="border-left:2px solid rgb(16, 16, 255);margin-left:5px;padding-left:5px">
<br>From: Felipe Almeida Lessa <<a href="mailto:felipe.lessa@gmail.com" target="_blank">felipe.lessa@gmail.com</a>><br>Subject: Re: [Haskell-cafe] Acquiring a random set of a specific size (w/o dups) from a range of Ints<br>
To: "michael rice" <<a href="mailto:nowgate@yahoo.com" target="_blank">nowgate@yahoo.com</a>><br>Cc: <a href="mailto:haskell-cafe@haskell.org" target="_blank">haskell-cafe@haskell.org</a><br>Date: Monday, June 13, 2011, 9:38 PM<div class="im">
<br><br><div>On Mon, Jun 13, 2011 at 8:56 PM, michael rice <<a rel="nofollow">nowgate@yahoo.com</a>> wrote:<br>> Is there an (existing) way to select 5 Ints randomly (no duplicates) from a population, say 1-20 (inclusive)?<br>
<br>Yes, already implemented in the monte-carlo package as sampleSubset [1],<br><br> sampleSubset :: MonadMC m => [a] -> Int
-> m [a]<br><br>Complete example code for your example:<br><br> evalMC (sampleSubset [1..20] 5) (mt19937 0)<br><br>Cheers!<br><br>[1] <a rel="nofollow" href="http://hackage.haskell.org/packages/archive/monte-carlo/0.4.1/doc/html/Control-Monad-MC-Class.html#v:sampleSubset" target="_blank">http://hackage.haskell.org/packages/archive/monte-carlo/0.4.1/doc/html/Control-Monad-MC-Class.html#v:sampleSubset</a><br>
<br>--<br>Felipe.<br></div></div></blockquote></div></td></tr></tbody></table></div></td></tr></tbody></table><br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>