f# - Turning a generator based on System.Random into an FsCheck generator -


suppose given generator based on system.random , want turn fscheck generator:

let mygen = mygen(system.random()) let fsgen = gen { return mygen.generate() } 

there couple of issues easy solution: first concept of size ignored; think not big issue though, many generators ignore size. other issue impacts reproducibility because fscheck generators pure functions under hood, randomness provided sampling mechanism in test runner. (this explained in this answer).

now, 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 performance penalty because have create new instance of mygen each time (with potentially high initialization cost)

any better way?

could following work? though mygen random in nature, can make deterministic fixing seed:

let deterministicgen = mygen(random(42)) 

due nature of random, isn't guaranteed have sufficiently random-like distribution, if purposes, can create deterministic sequence of values generated mygen:

let deterministicvalues = list.init 100 (fun _ -> deterministicgen.generate()) 

this 100 values, depending on needs, can create bigger sample set of 1000, or perhaps 10000 values.

just deterministicgen, deterministicvalues fixed: it's list of values generated mygen.

you can ask fscheck randomly pick values list:

let fsgen = gen.elements deterministicvalues 

here, fsgen gen<'a>, 'a whatever mygen.generate() returns.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -