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 188.8.131.52 (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):
liftM3 comes into play:
a3, correspond to the arguments of
r corresponds to the return value of
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:
lift3 function in F# can be written in terms of
Finally, here are some sample floats: