mercredi 9 septembre 2015

Random binary list of lists of numbers

I am trying to get a function that returns a list of lists of zeros or ones, obviusly in a random way.

Example:

getBinaryRandomList::Int->Int->[[Int]]

Prelude> getBinaryRandomList 4 3
[[1,0,0,1],[1,1,0,1],[0,0,0,1], [0,1,0,1]]

So far, i have done this functions:

--tuns an `Int` into a `[Int]`. The [Int] would represent a binary number
int2bin :: Int -> [Int]
int2bin 0 = []
int2bin n = mod n 2 : int2bin (div n 2)

I have got stucked here, it throws an error at compilation time:

--returns a random number
randomInt::(Int,Int)->Int
randomInt x y = do 
                newStdGen
                randomR(x, y) getStdGen

Compiling...

[1 of 1] Compiling Main             ( ag.hs, interpreted )

ag.hs:8:25: parse error on input `randomR'

The "main" function would be like this:

--n lists number
--d digit number
getBinaryRandomList::Int->Int->[[Int]]
getBinaryRandomList d 0 = []
getBinaryRandomList d n = take d (int2bin(randomInt(0,50))) : getBinaryRandomList(n-1)

My approach is the next:

  1. Coding a function that creates an aleatory Int number between 0 an n.
randomInt::(Int,Int)->Int

  1. Coding a function that converts those aleatory numbers into a list of binaries.
int2bin::Int->[Int]  % Already done

  1. Forming a list with those numbers
 getBinaryRandomList::Int->Int->[[Int]]

How could i implement that in Haskell?




Aucun commentaire:

Enregistrer un commentaire