lundi 20 juillet 2015

Using System.Random.Mersenne.Pure64 to generate a random Float in the range [0,1]

I do not want to use System.Random because it is significantly slower and I need to generate millions of random floats. I also cannot use System.Random.MWC because it is not pure.

I made an attempt at writing this myself but my solution does not generate a uniform range, all the values are very close to 0.

randomRMSP :: (Float,Float) -> PureMT -> (Float,PureMT)                   
randomRMSP (lo,hi) rng =
    let (f,rng') = first double2Float (randomDouble rng)
    in (lo + f / (maxFloat + 1) * (hi - lo + 1),rng')

maxRealFloat :: RealFloat a => a -> a                                           
maxRealFloat a = encodeFloat m n where                                          
    b = floatRadix a                                                            
    e = floatDigits a                                                           
    (_, e') = floatRange a                                                      
    m = b ^ e - 1                                                               
    n = e' - e

I'm pretty sure the maxRealFloat function is correct because it returns the correct values for Floats and Doubles according to Wikipedia




Aucun commentaire:

Enregistrer un commentaire