This post is part of a series of posts on implementing a minimal version of QuickCheck from scratch. The source code is available on GitHub.

In the previous post I’ve generated random characters. In this post, I’ll be doing the same for booleans.

Two generators are required for this, which have been ported already on previous posts:

  • a generator that lifts a value to the elevated world of generators; that’s the one ported as Gen.init
  • a generator that randomly uses one of the above generators, that’s Gen.oneof

Given the above, a boolean generator can be written as:

let bool =
    Gen.oneof [ Gen.init true
                Gen.init false ]

val bool : Gen<bool>

Finally, here are some sample booleans:

> Gen.bool |> Gen.generate;;
val it : bool = true

> Gen.bool |> Gen.generate;;
val it : bool = true

> Gen.bool |> Gen.generate;;
val it : bool = true

> Gen.bool |> Gen.generate;;
val it : bool = false

> Gen.bool |> Gen.generate;;
val it : bool = false

> Gen.bool |> Gen.generate;;
val it : bool = false

> Gen.bool |> Gen.generate;;
val it : bool = false

> Gen.bool |> Gen.generate;;
val it : bool = true

> Gen.bool |> Gen.generate;;
val it : bool = false

> Gen.bool |> Gen.generate;;
val it : bool = true

> Gen.bool |> Gen.generate;;
val it : bool = true

> Gen.bool |> Gen.generate;;
val it : bool = false

> Gen.bool |> Gen.generate;;
val it : bool = false