[Fluxus] picking a function with weights
evan.raskob [lists]
lists at lowfrequency.org
Tue Feb 2 06:29:20 PST 2010
The method I use is pretty similar, generating a random number based
on the sum of the weights, then subtracting the weights from it in
order until I hit (or go below) 0 which means the current weighted
item should be selected.
Although I suppose you could use what Dave said with multiple list
items, especially if you are just keeping track of integers, as they
are easily duplicated without losing much memory.
Cheers
Evan
On Feb 2, 2010, at 2:17 PM, Dave Griffiths wrote:
> 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