Generate random numbers without repeating a value: Difference between revisions
Content added Content deleted
(Added AppleScript.) |
(→{{header|Haskell}}: Added a Haskell solution.) |
||
Line 408: | Line 408: | ||
13 12 8 3 9 17 14 10 6 2 11 20 19 18 4 7 16 1 15 5 |
13 12 8 3 9 17 14 10 6 2 11 20 19 18 4 7 16 1 15 5 |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
<lang haskell>import Control.Monad (replicateM) |
|||
import Data.List (sortOn) |
|||
import Data.Ord (comparing) |
|||
import System.Random (randomRIO) |
|||
main :: IO () |
|||
main = do |
|||
let rnd = randomRIO (0, 1) :: IO Double |
|||
n = 20 |
|||
rank <- replicateM n rnd |
|||
print $ snd <$> sortOn fst (zip rank [1 .. n])</lang> |
|||
{{Out}} |
|||
For example: |
|||
<pre>[16,1,3,9,8,20,12,18,11,19,2,14,5,6,13,15,17,10,7,4]</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |