Hey all,
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.
Something like:
Gives an even chance to all values
/ / / /
The bigger the number, the more likely it is.
So X is the number, and Y is the probability.
100 increments from 0-1 is what I'll be using.
Thanks all, B.
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
Hallo, Mathieu Bouchard hat gesagt: // Mathieu Bouchard wrote:
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.
Yep, that's what's used in [list-wrandom]
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.
You made me curious: How would you recommend to make it faster for large lists?
Frank Barknecht _ ______footils.org__
On Sat, 13 Oct 2007, Frank Barknecht wrote:
Mathieu Bouchard hat gesagt: // Mathieu Bouchard wrote:
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.
You made me curious: How would you recommend to make it faster for large lists?
You make a binary search in the array. Suppose every element could be what you look for. Look at the value in the middle element. You know that elements are already sorted (because they are cumulative of a nonnegative function) so you can eliminate all possibilities on one of the sides of the middle element. Then you pick a new middle... You can go through a 1024-element array in 10 steps using just [<].
So, this is the real reason for cumulating: you can't use that trick if you don't cumulate.
_ _ __ ___ _____ ________ _____________ _____________________ ... | Mathieu Bouchard - tél:+1.514.383.3801, Montréal QC Canada
moin moin,
I think my [weightmap] external does pretty much what you're describing. It ought to be in cvs under externals/moocow/weightmap. Also, Yves' [probalizer] does basically the same thing, and also includes a GUI and a "training mode" for the weights.
marmosets, Bryan
On 2007-10-12 19:51:34, "B. Bogart" bbogart@goto10.org appears to have written:
Hey all,
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.
Thanks!
[unauthorized/probalizer][ is working great. Thank again Yves.
In pd-extended the right-click help does not work, I suppose since it looks for the reference patch in extra, not extra/unauthorized.
.b.
Bryan Jurish wrote:
moin moin,
I think my [weightmap] external does pretty much what you're describing. It ought to be in cvs under externals/moocow/weightmap. Also, Yves' [probalizer] does basically the same thing, and also includes a GUI and a "training mode" for the weights.
marmosets, Bryan
On 2007-10-12 19:51:34, "B. Bogart" bbogart@goto10.org appears to have written:
Hey all,
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.
Hallo, B. Bogart hat gesagt: // 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.
Not graphical, but [list]-abs has [list-wrandom] (also attached). It'd modelled after [random], but instead of the range, you send a list of numbers to the second inlet, that give relative probabilities for its indices/positions. If you bang the first inlet to get a random position out. The probs are automatically normalized. I don't think it's in pd-extended yet, but it's just an abstraction, so you can drop it in wherever you want (uses some other list-abs, though.)
Internally the list of probs is stored in a table, so if you rip it apart, you can directly read from a table.
Frank Barknecht _ ______footils.org__