[ create a new paste ] login | about

Link: http://codepad.org/GLIWxHae    [ raw code | output | fork ]

Haskell, pasted on Jan 17:
module Main where
import           Data.Bits
import           Data.Int

a32=7^5::Int32

a64::Int64
a64=7^5

m32=2^31-1::Int32

m64::Int64
m64= 2^31-1

(q32,r32)=m32 `divMod` a32

getRandom1:: Int64 -> Int64
getRandom1 xold = (a64*xold) `mod` m64

low31=0x7FFFFFFF::Int64

getRandom3 :: Int64 -> Int64
getRandom3 xold = if newmodded<m64 then newmodded else newmodded-m64
    where
        new = a64*xold
        newmodded=shiftR new 31 + (new .&. low31)

getRandom2:: Int32 -> Int32

getRandom2 xold = if res >0 then res else res +m32
  where
      (hi,lo)=xold `divMod` q32
      res = a32*lo-r32*hi

randoms1 = iterate getRandom1

randoms2 = iterate getRandom2

randoms3 = iterate getRandom3
 
numtest = 1000::Int


main :: IO ()
main = do
  let ones = take numtest . randoms1
  let twos = take numtest . map (fromInteger.fromIntegral) . randoms2
  let threes = take numtest . randoms3

  print (ones 1 == threes 1)
  print (twos 1==threes 1)


Output:
1
2
True
True


Create a new paste based on this one


Comments: