# Generators and the 'choose' function

21 February 2015

Test data is produced, and distributed, by test data generators.

Both QuickCheck and FsCheck provide default generators for primitive types, but it’s also possible to build custom generators for custom types.

Generators have types of the form `Gen a`

, where `a`

is the type of the test data to be produced.

## QuickCheck

Generators are built from the function `choose`

, which makes a random choice of a value from a range, with a uniform distribution in the closed interval *[a, a]*.

The type `Gen`

is an instance of Haskell’s `Monad`

. This involves implementing its minimal complete definition:

### Example: Take a random element from a list

Using *do* notation:

A possible translation into vanilla monadic code:

## FsCheck

Similarly, FsCheck defines the type `gen`

as a computation expression.

The `choose`

function is non-generic; instead it generates a 32-bit signed integer, with a uniform distribution in the closed interval *[l, h]*.

### Example: Take a random element from a list

Notice that `takeFromList`

has equivalent signature with the one in Haskell: