[racket] Knuth's algorithm S

From: Manfred Lotz (manfred.lotz at arcor.de)
Date: Mon Mar 10 15:55:41 EDT 2014

On Mon, 10 Mar 2014 15:09:28 -0400
Sean Kanaley <skanaley at gmail.com> wrote:

> The current Racket algorithm has an error:
> 
> (define counts (build-vector 10 identity))
> 
> This sets the counts to their indices, meaning e.g. 9 was pre-counted
> 9 times.  Switch the first line of the executable portion to run only
> say 3 trials to see the effects.  It should be:
> 
> (define counts (make-vector 10))
> 

Oops, sure. Corrected.

> Also, multiplying count by (random) should just be (random count):
> 
> (when (< (* count (random)) n)
>               (vector-set! vec (random n) item))))
> 
> becomes
> 
> (when (< (random count) n)
>               (vector-set! vec (random n) item))))
> 

I took over from the initial version. Actually I'm not quite sure if
this is an error. I believe those 2 versions are equivalent. However,
your version is much clearer. Therefore I adjusted it to your
suggestion.



Thanks for this.


-- 
Manfred



Posted on the users mailing list.