Skip to main content
ubuntuask.com

Back to all posts

How to Generate Different Random Values In Haskell?

Published on
5 min read
How to Generate Different Random Values In Haskell? image

Best Haskell Programming Guides to Buy in October 2025

1 Real World Haskell

Real World Haskell

  • AFFORDABLE PRICES FOR QUALITY READS-SAVE WHILE YOU ENJOY!
  • ENVIRONMENTALLY FRIENDLY-REDUCE WASTE BY CHOOSING USED BOOKS.
  • UNIQUE FINDS-DISCOVER OUT-OF-PRINT AND RARE TITLES EASILY!
BUY & SAVE
$24.40 $49.99
Save 51%
Real World Haskell
2 Learn You a Haskell for Great Good!: A Beginner's Guide

Learn You a Haskell for Great Good!: A Beginner's Guide

  • AFFORDABLE PRICES FOR QUALITY USED BOOKS, SAVING YOU MONEY!
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY BUYING PRE-LOVED BOOKS.
  • GUARANTEED GOOD CONDITION FOR A SATISFYING READING EXPERIENCE.
BUY & SAVE
$35.00 $44.95
Save 22%
Learn You a Haskell for Great Good!: A Beginner's Guide
3 Haskell in Depth

Haskell in Depth

BUY & SAVE
$57.13 $59.99
Save 5%
Haskell in Depth
4 Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

BUY & SAVE
$55.05 $57.95
Save 5%
Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming
5 Learn Haskell by Example (Bookcamp)

Learn Haskell by Example (Bookcamp)

BUY & SAVE
$51.84 $59.99
Save 14%
Learn Haskell by Example (Bookcamp)
6 Programming in Haskell

Programming in Haskell

BUY & SAVE
$42.99 $47.00
Save 9%
Programming in Haskell
+
ONE MORE?

In Haskell, you can generate different random values using the random and randomR functions from the System.Random module. Here are some ways to generate random values:

  1. Generating a Random Number: To generate a random number, you can use the random function. It takes a random number generator as input and returns a random value along with a new generator. Here's an example:

import System.Random

getRandomNumber :: IO Int getRandomNumber = do gen <- newStdGen -- Create a new random number generator let (num, newGen) = random gen :: (Int, StdGen) return num

  1. Generating a Random Number within a Range: If you want to generate a random number within a specific range, you can use the randomR function. It generates a random number between a given minimum and maximum value. Here's an example:

import System.Random

getRandomInRange :: Int -> Int -> IO Int getRandomInRange minValue maxValue = do gen <- newStdGen let (num, _) = randomR (minValue, maxValue) gen :: (Int, StdGen) return num

  1. Generating Random Booleans: You can also generate random boolean values using the random function. Here's an example:

import System.Random

getRandomBoolean :: IO Bool getRandomBoolean = do gen <- newStdGen let (bool, newGen) = random gen :: (Bool, StdGen) return bool

These are just a few examples of generating random values in Haskell. You can explore the System.Random module to find more functions and techniques for generating random values based on your specific requirements.

How to generate random words in Haskell?

There are various ways to generate random words in Haskell. One common approach is to use the System.Random module and generate random characters from a predefined set of alphabets.

Here's an example implementation:

import System.Random

randomWord :: Int -> IO String randomWord n = do gen <- newStdGen return $ take n $ randomRs ('a', 'z') gen

main :: IO () main = do word <- randomWord 5 -- generate a random word of length 5 putStrLn word

In this example, we define the randomWord function that takes an Int argument n, which represents the length of the random word to be generated. Inside the function, we first generate a new random generator using newStdGen function. Then, we use randomRs function to generate an infinite list of random characters between 'a' and 'z'. Finally, we use take function to extract n characters from the list and return the generated random word.

In the main function, we call randomWord with the desired length of the random word and print the result to the console.

You can modify the code as per your requirements, such as generating random words from a different character set or using a different random generator.

How to generate random file names in Haskell?

To generate random file names in Haskell, you can make use of the random package. Here's an example of how you can generate random file names:

import System.Random import System.FilePath

randomFileName :: IO FilePath randomFileName = do gen <- getStdGen let (name, newGen) = random gen :: (Int, StdGen) setStdGen newGen return $ "file" ++ show name ++ ".txt"

main :: IO () main = do fileName <- randomFileName putStrLn $ "Generated file name: " ++ fileName

In this example, we use the random function from the System.Random module to generate a random number. Then, we modify the standard random generator using setStdGen to ensure different random numbers are generated each time randomFileName is called.

The FilePath module is imported to deal with generating valid file paths. In this example, the file names will be of the form "fileN.txt", where N is the random number generated.

You can modify the file name generation logic to fit your specific needs.

How to generate random lottery numbers in Haskell?

To generate random lottery numbers in Haskell, you can make use of the System.Random module. Here's an example of a function that generates six unique random lottery numbers between 1 and 49:

import System.Random (randomRs, newStdGen) import Data.List (nub)

-- Generate 6 unique random lottery numbers between 1 and 49 generateLotteryNumbers :: IO [Int] generateLotteryNumbers = do gen <- newStdGen let numbers = take 6 $ nub $ randomRs (1, 49) gen return numbers

In this example, we import the necessary functions from System.Random and Data.List. The randomRs function generates an infinite list of random numbers based on the given range (1, 49), using the provided random generator (gen). We then use the nub function to remove duplicate numbers, and take 6 to select the first six unique numbers. Finally, the list of numbers is returned as an IO action, allowing you to use it in the IO monad.

How to generate a random element from a list in Haskell?

There are multiple ways to generate a random element from a list in Haskell. One common approach is to use the System.Random module. Here's an example using the randomRIO function:

import System.Random (randomRIO)

randomElement :: [a] -> IO a randomElement xs = do index <- randomRIO (0, length xs - 1) return (xs !! index)

In this example, randomElement takes a list xs and uses randomRIO to generate a random index within the valid range of indices for xs. It then returns the element at that index using the (!!) operator.

You can use this function as follows:

main :: IO () main = do let myList = [1, 2, 3, 4, 5] randomVal <- randomElement myList putStrLn ("Random element: " ++ show randomVal)

The randomElement function is invoked with myList as the input list, and the resulting random value is printed to the console.