Write you some QuickCheck - Generating random long integers

Wed, 17 Feb 2016 00:00:00 UTC

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.

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

The long integers are going to be generated by the integer returned by the first generator and then multiplied by a 16-bit1 integer’s largest possible value.

Given the above, a long integer generator can be written as:

/// <summary>
/// Generates a 64-bit integer (with absolute value bounded by the generation
/// size multiplied by 16-bit integer's largest possible value).
/// </summary>
let int64 = Gen.int |> Gen.map (fun n -> Operators.int64 (n * 32767))

val int64 : Gen<int64>

Finally, here are some sample long integers:

> Gen.int64 |> Gen.generate;;
val it : int64 = -294903L

> Gen.int64 |> Gen.generate;;
val it : int64 = -32767L

> Gen.int64 |> Gen.generate;;
val it : int64 = 163835L

> Gen.int64 |> Gen.generate;;
val it : int64 = 131068L

> Gen.int64 |> Gen.generate;;
val it : int64 = -65534L

> Gen.int64 |> Gen.generate;;
val it : int64 = 229369L

> Gen.int64 |> Gen.generate;;
val it : int64 = 393204L

  1. To be more precise, I should use a 32-bit integer’s largest possible value, but then the generated numbers become very big and less interesting.