Skip to content

Instantly share code, notes, and snippets.

@Fristi
Last active December 14, 2015 16:38
Show Gist options
  • Save Fristi/5116113 to your computer and use it in GitHub Desktop.
Save Fristi/5116113 to your computer and use it in GitHub Desktop.
module Utils =
let private rng = new Random()
let rec PickRandomPair<'TKey when 'TKey : equality> (pool:('TKey * int) list) =
let decreasePair(needle:'TKey, pool:('TKey * int) list) =
let decrease (key:'TKey, value:int) = if key = needle then (key, value - 1) else (key, value)
pool |> Seq.fold (fun acc r -> (decrease(r) :: acc)) []
let i = rng.Next(0, pool.Count())
let (key, value) = pool.[i]
match value with
| 0 -> PickRandomPair(pool)
| _ -> (key, decreasePair(key, pool))
[<TestFixture>]
type Geometry() =
[<Test>]
member this.TestRandomPair() =
let pairs = [
(1, 2);
(2, 2)
]
let (r, npairs) = pickRandomPair(pairs)
let countTwo = query {
for (n, quantity) in npairs do
where (quantity = 2)
count
}
Assert.IsTrue(r = 1 || r = 2)
Assert.AreEqual(1, countTwo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment