Defining custom generators with PropCheck

Hi @alfert,

Can you recommend a way to generate a sublist of N unique elements from a list?

I have a couple of ideas but none seem to be very efficient. For example, to generate 8 unique numbers between 1 and 104:

Solution 1

such_that numbers <- vector(8, integer(1, 104)), when: uniques?(numbers) #define the uniques?/1 helper too

Solution 2
Make a recursive generator that generate a number N0 from 1 to (104 - 8), then generate N1 from (N0 + 1) to (104 - 7), then generate N2 from (N1 + 1) to (104 - 6), etc…
My problem with this one:

  • it seems hard to write such a generator
  • it is a bit complex
  • it skews the generated numbers toward the higher range and is unlikely to be uniform

Solution 3
This is my favorite but I’m not sure how to write it as a generator:

def uniques_numbers() do
  1..104
  |> Enum.shuffle()
  |> Enum.take(8)
end