*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 this post I’ll be generating random 32-bit floating-point numbers.

In Haskell’s QuickCheck 1.2.0.1 (but also in version 2.8), random 32-bit floating-point numbers are generated as shown below:

Two things are important in the above code:

Here’s the original `fraction`

function in Haskell:

It takes 3 integers and returns an `a`

which is a fractional (a numeric type that supports the ordinary division operator `/`

).

Here are some sample values generated by fraction:

Now, what I want to do is to call the `fraction`

function passing *random* integers, something like this (pseudo-code):

Here’s where `liftM3`

comes into play:

`a1`

, `a2`

, and `a3`

, correspond to the arguments of `fraction`

`r`

corresponds to the return value of `fraction`

`m a1`

, `m a2`

, `m a3`

will be 3 generators for integers
`m r`

the return value (the random float).

So, in F#, a generator for floats can be written as:

And the `lift3`

function in F# can be written in terms of `apply`

and `return`

:

Finally, here are some sample floats: