[Fluxus] picking a function with weights

Dave Griffiths dave at pawfal.org
Tue Feb 2 06:17:18 PST 2010


Hi Kassen,

Without weights:

(define (choose l) 
  (list-ref l (random (length l))))

(define funcs
  (list
   (lambda () (display "one"))
   (lambda () (display "two"))
   (lambda () (display "three"))
   (lambda () (display "four"))
   (lambda () (display "five"))))

((choose funcs))

I generally do weighting by duplicating list elements, but there must be
a better way?

cheers,

dave

On Tue, 2010-02-02 at 15:08 +0100, Kassen wrote:
> Dear list,
> 
> 
> Another -probably simple- Scheme question. Suppose we wanted to
> execute one (and only one) of a few functions, pick this one randomly
> and assign weights to them. For example we might like to populate a
> world with mostly cubes, some spheres and just a few cylinders. How
> would we go about this cleanly? I could nest a train of "if"
> statements that would compare a (rndf) to a constant but that's hardly
> convenient and leads to ugly code. I could also set up a few (cond)
> statements to use with a single random number but since we are
> basically dealing with picking from a list I could imagine Scheme,
> which is specialised in lists, would have some specialised trick up
> it's sleeve that might serve?
> 
> 
> Yours,
> Kas.




More information about the Fluxus mailing list