`oneOf` and `elements` never yield the last element
sharkdp opened this issue · comments
Currently, oneOf
is implemented as
oneOf :: forall a. Gen a -> Array (Gen a) -> Gen a
oneOf x xs = do
n <- chooseInt zero (length xs)
if n < one then x else fromMaybe x (xs !! (n - one))
Apparently, chooseInt a b
returns a random integer from the set a, a + 1, ..., b - 1
, excluding the value b
. Consequently, the maximum value for the index n - one
in the last line is length xs - 2
and the last element of xs
will never be chosen:
> showSample (oneOf (pure 1) [pure 2])
[1,1,1,1,1,1,1,1,1,1]
The same is true for elements
.
👍 Thanks!