On Fri, 12 Oct 2007, B. Bogart wrote:
Does someone have a very simple implementation of a weighted randomness
patch where the distribution of the numbers is read from a table? I see
Miller's moses example, but I would like more graphic control of density.
First, the sum all the weights should be 1. If it doesn't, or if you
don't know in advance, sum all the weights.
Then build a second table, in which each element is the sum all
(inclusively) previous elements of the first table. This is the sum of the
previous element of the second table and the current element of the first
table. This second table is the cumulative distribution.
Then generate a random number between 0 included and the sum of all
weights excluded. Finally, scan the table from 0 upwards until you hit any
number above the random number. That index is the output value.
You don't absolutely need the second table, but it does save a bit of time
afterwards.
If ever you need a much larger table, then it may become too slow, and if
so, then write again and I'll explain how to accelerate it.
_ _ __ ___ _____ ________ _____________ _____________________ ...
| Mathieu Bouchard - tél:+1.514.383.3801, Montréal QC Canada
_______________________________________________
[email protected] mailing list
UNSUBSCRIBE and account-management ->
http://lists.puredata.info/listinfo/pd-list