Suppose I am given a generator based on System.Random
and I want to turn it into an FsCheck generator:
let myGen = MyGen(System.Random())
let fsGen = gen { return myGen.Generate() }
There are a couple of issues with this easy solution: the first is that the concept of size is ignored; I think it is not a big issue though, many generators ignore the size. The other issue impacts reproducibility because FsCheck generators are pure functions under the hood, the randomness is provided only by the sampling mechanism in the test runner. (this is clearly explained in this answer).
Now, a solution may be:
let fsGen =
gen {
let! seed = Gen.choose(0, System.Int32.MaxValue)
let myGen = MyGen(System.Random(seed))
return myGen.Generate() }
but there is a performance penalty because I have to create a new instance of MyGen
each time (with a potentially high initialization cost)
Any better way?