[racket] Neil: Is there a sensible way to sample from the naturals? EOM

From: Sam Tobin-Hochstadt (samth at cs.indiana.edu)
Date: Wed Feb 19 17:33:42 EST 2014

On Wed, Feb 19, 2014 at 5:02 PM, Neil Toronto <neil.toronto at gmail.com> wrote:
>
>   (: random-natural/no-mean (-> Real Natural))
>   (define (random-natural/no-mean prob-zero)
>     (define n (exact-floor (sample (geometric-dist prob-zero))))
>     (define m1 (assert (expt 2 n) integer?))
>     (define m0 (quotient m1 2))
>     (max 0 (random-integer m0 m1)))
>
> The "max 0" keeps TR from complaining that `random-integer' returns an
> Integer.

These both look like places where `math` could do better here with types.

Specifically, `geometric-dist` should probably say that it produces
non-negative floats.  That would fix the need for the `assert`.

Second, `random-integer` could have the type: (case-> (Natural Natural
-> Natural) (Integer Integer -> Integer)).  That would fix the need
for `max`.

Finally, I don't understand why `geometric-dist` returns floats and
not integers, anyway.

Sam

Posted on the users mailing list.