Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/Control/Monad/Eff/Random.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ or if either of `low` or `high` is not an integer.

For example:
``` purescript
randomInt (fromNumber 1) (fromNumber 10) >>= Console.print
randomInt 1 10 >>= Console.print
```
will print a random integer between 1 and 10.

Expand All @@ -46,7 +46,7 @@ value (exclusive). It is unspecified what happens if `maximum < minimum`.

For example:
``` purescript
randomRange 1 2 >>= Console.print
randomRange 1.0 2.0 >>= Console.print
```
will print a random number between 1 and 2.

Expand Down
10 changes: 6 additions & 4 deletions src/Control/Monad/Eff/Random.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ foreign import random :: forall e. Eff (random :: RANDOM | e) Number
-- |
-- | For example:
-- | ``` purescript
-- | randomInt (fromNumber 1) (fromNumber 10) >>= Console.print
-- | randomInt 1 10 >>= Console.print
-- | ```
-- | will print a random integer between 1 and 10.
randomInt :: forall e. Int -> Int -> Eff (random :: RANDOM | e) Int
randomInt low high = do
n <- random
pure <<< U.fromJust <<< fromNumber <<< Math.floor $ toNumber ((high - low + one) + low) * n
pure <<< U.fromJust <<< fromNumber <<< Math.floor $ toNumber (high - low + one) * n + toNumber low

-- | Returns a random number between a minimum value (inclusive) and a maximum
-- | value (exclusive). It is unspecified what happens if `maximum < minimum`.
-- |
-- | For example:
-- | ``` purescript
-- | randomRange 1 2 >>= Console.print
-- | randomRange 1.0 2.0 >>= Console.print
-- | ```
-- | will print a random number between 1 and 2.
randomRange :: forall e. Number -> Number -> Eff (random :: RANDOM | e) Number
randomRange min max = (((max - min) + min) *) <$> random
randomRange min max = do
n <- random
return (n * (max - min) + min)

-- | Returns a random boolean value with an equal chance of being `true` or
-- | `false`.
Expand Down