moodmosaic (Nikos Baxevanis)
Blog - About - Open Source - Bookmarks - Tags

Write you some QuickCheck - Generating random long integers

Wednesday, 17 February 2016

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. 

━━━

This post has been filed under #f# #quickcheck

Feedback

Have feedback on this page? Let @nikosbaxevanis know on twitter. Need help or found a bug? Contact me.


© 2011-2018 Nikos Baxevanis. All written content on this site is provided under a Creative Commons ShareAlike license. All code is provided under a MIT license unless otherwise stated.