Last active
December 14, 2015 16:38
-
-
Save Fristi/5116113 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<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