Last active
January 4, 2016 00:19
-
-
Save oropon/8540769 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Main where | |
import System.Random | |
import Data.Maybe | |
import Data.Bits | |
import Data.List | |
import Data.Char | |
main = do | |
gen <- getStdGen | |
print $ nub $ take 1000 $ barcodes avail_chrs 8 (randomRs (0,maxBound::Int) gen :: [Int]) | |
avail_chrs :: [Char] | |
avail_chrs = filter (/= 'O') ['A'..'Z'] | |
barcodes :: [Char] -> Int -> [Int] -> [[Char]] | |
barcodes comps len ns = (map (avail_chrs !!) (seg ++ [check_digit])) : barcodes comps len segs | |
where | |
seg = map (flip mod $ length avail_chrs) $ take (len-1) ns | |
segs = drop (len-1) ns | |
check_digit = get_check_digit (length avail_chrs) seg | |
get_check_digit :: Int -> [Int] -> Int | |
get_check_digit max = flip mod max . foldl1 xor | |
unique :: (Eq a) => [a] -> [a] | |
unique [] = [] | |
unique (x:xs) | |
| x `elem` xs = unique xs | |
| otherwise = x: unique xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment