7 | interface Random a where
8 | randomIO : HasIO io => io a
13 | randomRIO : HasIO io => (a, a) -> io a
15 | %foreign "scheme:blodwen-random"
16 | "javascript:lambda:(max)=>Math.floor(Math.random() * max)"
17 | prim__randomBits32 : Bits32 -> PrimIO Bits32
19 | randomBits32 : Bits32 -> IO Bits32
20 | randomBits32 upperBound = fromPrim (prim__randomBits32 upperBound)
26 | let maxInt : Bits32 = 2147483647
27 | negMinInt : Bits32 = 2147483648
28 | magnitude : Bits32 = maxInt + negMinInt
29 | bits32 <- liftIO $
randomBits32 magnitude
30 | let int : Integer = cast bits32
31 | pure . cast $
int - (cast negMinInt)
34 | randomRIO (lo, hi) =
35 | let range : Integer = (cast hi) - (cast lo) + 1
36 | in pure . cast $
!(liftIO . randomBits32 $
cast range) + cast lo
38 | %foreign "scheme:blodwen-random"
39 | "javascript:lambda:()=>Math.random()"
40 | prim__randomDouble : PrimIO Double
42 | randomDouble : IO Double
43 | randomDouble = fromPrim prim__randomDouble
48 | randomIO = liftIO randomDouble
51 | randomRIO (lo, hi) = map ((+ lo) . (* (hi - lo))) (liftIO randomDouble)
53 | %foreign "scheme:blodwen-random-seed"
54 | prim__srand : Bits64 -> PrimIO ()
58 | srand : Bits64 -> IO ()
59 | srand n = fromPrim (prim__srand n)
65 | rndFin : HasIO io => (n : Nat) -> io (Fin (S n))
68 | let intBound = the Int32 (cast (S k))
69 | randomInt <- randomRIO (0, intBound)
70 | pure $
restrict (S k) (cast randomInt)
74 | rndSelect' : HasIO io => {k : Nat} -> Vect (S k) a -> io a
75 | rndSelect' xs = pure $
Vect.index !(rndFin k) xs
79 | rndSelect : HasIO io => (elems : List a) -> (0 _ : NonEmpty elems) => io a
80 | rndSelect (x :: xs) = rndSelect' $
fromList (x :: xs)