unisonweb / base

Unison base libraries
https://share.unison-lang.org/@unison/base
18 stars 6 forks source link

Random.natIn that is inclusive of upper value #170

Open ceedubs opened 1 year ago

ceedubs commented 1 year ago

I would like to generate a Nat with an inclusive upper value of maxNat. Is there a straightforward way to do that currently? Since natIn lower upper is exclusive on upper, you can't do natIn lower maxNat. For cases other than maxNat, you could do natIn lower (increment upper), but this will overflow and give an error if upper is maxNat.

See also #169.

pchiusano commented 1 year ago

@ceedubs you can use Random.nat to generate Nat in the full range 0 to maxNat, does that help?

ceedubs commented 1 year ago

@pchiusano yes, but it's a bit annoying. In my current use-case the upper bound is passed in as an argument to the function, so I'll have to check whether the upper bound is maxNat and use nat if it is and natIn if it is not. If the lower bound were also passed in as an argument I think that I'd still have the same trouble.