about summary refs log tree commit diff
path: root/src/Xanthous/Random.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Xanthous/Random.hs')
-rw-r--r--src/Xanthous/Random.hs15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/Xanthous/Random.hs b/src/Xanthous/Random.hs
index 3cb0b068d3d7..41c80ab73c4c 100644
--- a/src/Xanthous/Random.hs
+++ b/src/Xanthous/Random.hs
@@ -9,6 +9,7 @@ module Xanthous.Random
   , evenlyWeighted
   , weightedBy
   , subRand
+  , chance
   ) where
 --------------------------------------------------------------------------------
 import Xanthous.Prelude
@@ -85,3 +86,17 @@ instance (Num w, Ord w, Distribution Uniform w, Excludable w) => Choose (Weighte
 
 subRand :: MonadRandom m => Rand StdGen a -> m a
 subRand sub = evalRand sub . mkStdGen <$> getRandom
+
+-- | Has a @n@ chance of returning 'True'
+--
+-- eg, chance 0.5 will return 'True' half the time
+chance
+  :: (Num w, Ord w, Distribution Uniform w, Excludable w, MonadRandom m)
+  => w
+  -> m Bool
+chance n = choose $ weightedBy (bool 1 (n * 2)) bools
+
+--------------------------------------------------------------------------------
+
+bools :: NonEmpty Bool
+bools = True :| [False]