[Haskell-cafe] Generating random tuples

Nick Bowler nbowler at elliptictech.com
Mon Nov 1 14:55:22 EDT 2010

On 2010-11-01 19:18 +0100, Jacek Generowicz wrote:
> I'm toying with generating random objects (for example tuples) and  
> started wondering what pearls of wisdom Cafe might have on the matter.  
> Two obvious points (relating to my toy code, shown below) are
> 1) The meaning of the limits required by randomR is not obvious for  
> types such as tuples (you could come up with some definition, but it  
> wouldn't be unique: how would you allow for different ones?[*]; you  
> might decide that having such limits is nonsensical and not want to  
> provide a randomR: would you then leave it undefinded?).

Indeed, the Random class has a fairly narrow "everything fits on the
real line" view of the world: not only is the talk about closed
intervals ambiguous in general, but so is the talk about uniform
distributions on those intervals.  That being said, there is an Ord
instance for tuples (a lexicographic ordering) and for this case I think
it would make the most sense to use that: select an element from the set
{ x : lo <= x <= hi }

Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)

More information about the Haskell-Cafe mailing list