3 ms·
It's not too bad to manually thread a seed around. import qualified Data.Vector.Mutable as V import Control.Monad (when) import qua
by tome 9y ago
It's not too bad to manually thread a seed around.
import qualified Data.Vector.Mutable as V
import Control.Monad (when)
import qualified System.Random as R
import Control.Monad.ST
st = runST $ do
v <- V.replicate 10 0
setIncreasing v
addRandomness v
printThemAll v = loop 0 (V.length v)
where loop i n = do
when (i < n) $ do
e <- V.read v i
print e
loop (i + 1) n
setIncreasing v = loop 0 (V.length v)
where loop i n = do
when (i < n) $ do
V.write v i (i * 2)
loop (i + 1) n
addRandomness v = loop initial_g 0 (V.length v)
where seed = 1234
initial_g = R.mkStdGen 1234 -- Seed
loop g i n = do
when (i < n) $ do
let (r, next_g) = R.random g
e <- V.read v i
V.write v i (e + r)
loop next_g (i + 1) n
- cousin_it 9y agoI see, thanks! The imperative version would still look more direct to me, but your versions are very nice.