How to generate random numbers in Haskell using random-fu with platform agnostic code?

I can't figure out how to use Data.Random.Source.IO to generate random numbers in a multi-platform way.

I can generate random numbers on Unix using Data.Random.Source.DevRandom , and the GitHub documentation for Windows uses Data.Random.Source.MWC , but there is no sample code to use Data.Random.Source.IO .

+4
source share
1 answer

Ok, I applied the github example to use Source.IO

 import Data.Random import Data.Random.Source.IO logNormal :: Double -> Double -> RVar Double logNormal mu sigmaSq = do x <- normal mu sigmaSq return (exp x) main = sample (logNormal 5 1) >>= print 

You can see in source Data.Random.Source.IO that it simply determines the appropriate instance for MonadRandom IO .

You can create a uniform random number from the list as

 import Data.Random import Data.Random.Source.IO main = sample (randomElement [0..9]) >>= print 
+4
source

Source: https://habr.com/ru/post/1434098/


All Articles