Created
October 22, 2014 18:29
-
-
Save codedmart/5580962237c262e52afa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Random | |
-- takes a random generator and returns a list of strings of 50 chars | |
start_population :: (RandomGen g) => g -> [[Char]] | |
start_population gen = | |
[take 50 $ randomRs ('A', 'z') gen | x <- [0..]] | |
main = do | |
randomGen <- newStdGen -- get a random generator | |
print $ take 2 $ start_population randomGen -- use it as a function argument |
but the problem is, that the RandomGen doesnt get updated, so every 50 char string from start_population is the same. instead you need to pull 50 char chunks from the original randomRs call like so:
import System.Random
-- from split Data.List.Split
build :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build g = g (:) []
chunksOf :: Int -> [e] -> [[e]]
chunksOf i ls = map (take i) (build (splitter ls)) where
splitter :: [e] -> ([e] -> a -> a) -> a -> a
splitter [] _ n = n
splitter l c n = l `c` splitter (drop i l) c n
-- takes a random generator and returns a list of strings of 50 chars
start_population :: (RandomGen g) => g -> [[Char]]
start_population gen =
chunksOf 50 $ map (chars !!) $ randomRs (0, l-1) gen
where chars = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9']
l = length chars - 1
main = do
randomGen <- newStdGen -- get a random generator
mapM_ print $ take 2 $ start_population randomGen -- use it as a function argument
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maybe like this