Skip to content

Instantly share code, notes, and snippets.

@davidgrenier
Created June 21, 2012 14:24
Show Gist options
  • Save davidgrenier/2966027 to your computer and use it in GitHub Desktop.
Save davidgrenier/2966027 to your computer and use it in GitHub Desktop.
Thread-safe random
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