サンプリングしたいアイテムの配列があります。
私は、Set は、元のセットを返すか、取得した要素が欠落している変更されたセットを返すかどうかに応じて、サンプルを抽出するのに適した構造であるという印象を受けました。ただし、セットから要素を直接取得する方法はないようです。
私が欠けているものはありますか?position < Set.count
または、ランダムに開始してメンバーが見つかるまで上昇するサロゲート関数とともに、一連のインデックスを使用する必要がありますか?
つまり、この線に沿った何か
module Seq =
let modulo (n:int) start =
let rec next i = seq { yield (i + 1)%n ; yield! next (i+1)}
next start
module Array =
let Sample (withReplacement:bool) seed (entries:'T array) =
let prng, indexes = new Random(seed), Set(Seq.init (entries |> Array.length) id)
Seq.unfold (fun set -> let N = set |> Set.count
let next = Seq.modulo N (prng.Next(N)) |> Seq.truncate N |> Seq.tryFind(fun i -> set |> Set.exists ((=) i))
if next.IsSome then
Some(entries.[next.Value], if withReplacement then set else Set.remove next.Value set)
else
None)
編集:まだ提供できるものを追跡するのではなく、私が提供したものを積極的に追跡すると、よりシンプルで効率的になります。