In the previous post I’ve generated random bytes. In this post, I’ll be generating random characters.

I’m going to need a couple of generators for this:

one that picks numbers from the range (in HEX)^{1}

[20h..7Eh] for ASCII printable characters

[80h..FFh] for ASCII extended characters

a generator that randomly uses one of the above generators

a generator that applies a function (in this case Operators.char<^T> to the above)

Porting Gen’s oneof

/// <summary>/// Randomly uses one of the given generators./// </summary>/// <param name="gens">The input list of generators to use.</param>/// <remarks>/// The input list must be non-empty./// </remarks>letoneofgens=letjoinx=Gen.bindxidjoin(Gen.elementsgens)

Porting Gen’s elements since oneof depends on it

/// <summary>/// Generates one of the given values./// </summary>/// <param name="xs">The input list.</param>/// <remarks>/// The input list must be non-empty./// </remarks>letelementsxs=// http://stackoverflow.com/a/1817654/467754letflipfxy=fyxGen.choose(0,(Seq.lengthxs)-1)|>Gen.map(flipSeq.itemxs)

All the pieces are now in place, and so a char generator can be written as: