Skip to content

Instantly share code, notes, and snippets.

@voldien
Created October 30, 2020 13:46
Show Gist options
  • Save voldien/ec340863cd7e886041ed965150659de2 to your computer and use it in GitHub Desktop.
Save voldien/ec340863cd7e886041ed965150659de2 to your computer and use it in GitHub Desktop.
Random sequence generator program
{-|
Random sequence generator program
Copyright (C) 2017 Valdemar Lindberg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-}
import System.Exit
import System.Random
import System.Environment
import System.IO
import Data.Time
-- Check if argument sequence is valid.
checkArgument :: [String] -> Bool
checkArgument args = do
let mi = read (args!!0) :: Int
let mx = read (args!!1) :: Int
let n = read (args!!2) :: Int
if mi > mx
then False
else if n < 0
then False
else True
-- Get current time of epoch.
getTime :: IO Int
getTime = do
currTime <- getCurrentTime
let timed = floor $ utctDayTime currTime
return timed
-- Generate random number sequence
generateRandom :: [String] -> IO [String]
generateRandom args = do
let mi = read (args!!0) :: Int
let mx = read (args!!1) :: Int
let n = read (args!!2) :: Int
seed <- getTime
let g = mkStdGen seed
return [ show x | x <- (take n (randomRs (mi, mx) g))]
main = do
args <- getArgs
if length(args) /= 3
then do
print("Requires 3 argument | min max n.\n")
exitFailure
else do
if not (checkArgument args)
then do
print("Invalid arguments.\n")
exitFailure
else do
randseq <- ( generateRandom args )
mapM_ putStrLn randseq
exitSuccess
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment