Skip to content

Commit 6e0ca41

Browse files
committed
Merge pull request #12 from hdgarrood/random-int-overflow
Fix overflows in randomInt
2 parents 2979e0b + 0102d64 commit 6e0ca41

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
],
2222
"dependencies": {
2323
"purescript-eff": "^0.1.0",
24-
"purescript-integers": "^0.2.0",
24+
"purescript-integers": "^0.2.1",
2525
"purescript-math": "^0.2.0"
2626
}
2727
}

src/Control/Monad/Eff/Random.purs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ module Control.Monad.Eff.Random where
33
import Prelude
44

55
import Control.Monad.Eff (Eff())
6-
import Data.Int (fromNumber, toNumber)
7-
8-
import qualified Data.Maybe.Unsafe as U
6+
import Data.Int (fromNumber, toNumber, floor)
97

108
-- | The `RANDOM` effect indicates that an Eff action may access or modify the
119
-- | JavaScript global random number generator, i.e. `Math.random()`.
@@ -28,7 +26,8 @@ foreign import random :: forall e. Eff (random :: RANDOM | e) Number
2826
randomInt :: forall e. Int -> Int -> Eff (random :: RANDOM | e) Int
2927
randomInt low high = do
3028
n <- random
31-
pure <<< U.fromJust <<< fromNumber <<< Math.floor $ toNumber (high - low + one) * n + toNumber low
29+
let asNumber = (toNumber high - toNumber low + one) * n + toNumber low
30+
return $ floor asNumber
3231

3332
-- | Returns a random number between a minimum value (inclusive) and a maximum
3433
-- | value (exclusive). It is unspecified what happens if `maximum < minimum`.

0 commit comments

Comments
 (0)