Created
June 21, 2012 14:24
-
-
Save davidgrenier/2966027 to your computer and use it in GitHub Desktop.
Thread-safe random
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
let next = | |
let result = ref 0 | |
let rnd = System.Random() | |
let rec roll () = | |
let old = !result | |
let next = rnd.Next() | |
if System.Threading.Interlocked.CompareExchange(result, next, old) = old then | |
next | |
else | |
roll() | |
roll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment