dimanche 24 novembre 2019

Generate unique lists of random integers within range

I am trying to write a function that will take two parameters, one that is the upper bound on the range that the random integer should be in. The other parameter is the number of lists of random numbers to be generated. Right now I am just trying to be able to generate lists of unique random numbers in a given range.

Using the following code, I can generate a random list of numbers, however, the numbers are of type randomList 5 (0,10) :: IO [Int] and not [Int]. The code also does not ensure that an integer is not repeated in a list.

import Test.QuickCheck

t :: Int -> (Int,Int) -> Gen [Int]
t n r = vectorOf n (choose r)

randomList n r = head `fmap` (sample' $ t n r)

I have looked at various StackOverflow answers although I still cannot find a solution. Many of the answers can only generate one random list with the same seed using System.Random




Aucun commentaire:

Enregistrer un commentaire