0 people like it.

Random Subset

A function that takes a random subset from a seq<'T>.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
open System
open System.Collections.Generic

let takeRandomly count lst =
    let random = Random()
    let makeList (xs : 'T seq) = List(xs)
    let list = makeList lst

    let rec takeRandomly count (list : 't List) lst acc =
        match count = acc with
            | true  -> lst
            | false ->
                match list.Count with
                    | 0 -> lst
                    | c ->
                        let c' = c - 1
                        let rand = random.Next(0, c')
                        let item = list.[rand]
                        list.RemoveAt rand
                        let lst' = item :: lst
                        takeRandomly count list lst' (acc + 1)
        
    takeRandomly count list [] 0

// Example:
let list = takeRandomly 50 [0 .. 1000]

// Output:
// val list : int list =
//   [176; 24; 351; 958; 102; 127; 612; 133; 237; 264; 30; 903; 395; 687; 594;
//    459; 80; 707; 391; 210; 953; 144; 685; 536; 329; 437; 659; 129; 948; 758;
//    113; 532; 381; 74; 284; 222; 721; 892; 435; 423; 580; 590; 875; 462; 214;
//    414; 235; 561; 279; 804]
namespace System
namespace System.Collections
namespace System.Collections.Generic
val takeRandomly : count:int -> lst:seq<'a> -> 'a list

Full name: Script.takeRandomly
val count : int
val lst : seq<'a>
val random : Random
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
Random() : unit
Random(Seed: int) : unit
val makeList : (seq<'T> -> List<'T>)
val xs : seq<'T>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
Multiple items
type List<'T> =
  new : unit -> List<'T> + 2 overloads
  member Add : item:'T -> unit
  member AddRange : collection:IEnumerable<'T> -> unit
  member AsReadOnly : unit -> ReadOnlyCollection<'T>
  member BinarySearch : item:'T -> int + 2 overloads
  member Capacity : int with get, set
  member Clear : unit -> unit
  member Contains : item:'T -> bool
  member ConvertAll<'TOutput> : converter:Converter<'T, 'TOutput> -> List<'TOutput>
  member CopyTo : array:'T[] -> unit + 2 overloads
  ...
  nested type Enumerator

Full name: System.Collections.Generic.List<_>

--------------------
List() : unit
List(capacity: int) : unit
List(collection: IEnumerable<'T>) : unit
Multiple items
val list : List<'a>

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val takeRandomly : (int -> List<'t> -> 't list -> int -> 't list)
Multiple items
val list : List<'t>

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val lst : 't list
val acc : int
property List.Count: int
val c : int
val c' : int
val rand : int
Random.Next() : int
Random.Next(maxValue: int) : int
Random.Next(minValue: int, maxValue: int) : int
val item : 't
List.RemoveAt(index: int) : unit
val lst' : 't list
Multiple items
val list : int list

Full name: Script.list

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
Raw view New version

More information

Link:http://fssnip.net/9C
Posted:6 years ago
Author:Taha Hachana
Tags: list , array , seq , recursion , random , collections