You could use Seq.nth

What is the type of your initial collection ? If if it an array you could use something like :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 

#light

let rnd = new System.Random()

let fill inData n =
  if n > Array.length inData then invalid_arg "len"
  let unique =
    inData
    |> Seq.distinct   //remove duplicate values
    |> Seq.to_array
  let res = Array.zero_create n
  for i in 0 .. n - 1 do
    let upperBound = (unique.Length - 1) - i
    let idx = rnd.Next(upperBound)
    //store the random value, and swap the values within [unique]
    //so that a chosen value can't be selected twice
    res.[ i ] <- unique.[idx]
    unique.[idx] <- unique.[upperBound]
    unique.[upperBound] <- res.[ i ]
  res
By on 10/2/2008 9:44 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper