weighted random a la SC "[1,2,4].wchoose([0.5,0.3,0.1]);" in Max

kjg's icon

Hello,

What would be the simplest way to implement a weighted random in Max, comparable to the following line of SuperCollider code?

[1,2,4].wchoose([0.5,0.3,0.1]);

The line results in a value of 1, 2 or 3, where the weighting is:
1 five times more often than 4, and 2 three times more often than 4.

In SC the values for the weighting don't have to add up to one, they are automatically normalized. They just have to be smaller than 1.

I am aware of the possibilities of the [table] object, but am looking for something a bit cleaner, that can use floats et cetera. Ideally an object that would take two lists (values/weightings) and output a value when banged.

Any thoughts would be appreciated.

Regards,
Klaas-Jan

f's icon

good example of where syntax shines and patching gets ugly.
(although you're wrong about the auto normalisation - that'd be
[1,2,4].wchoose([0.5,0.3,0.1].normalizeSum); to be really picky)
simplest i don't know, but one way to do it would be with chained
[split] or [if] statements. of course this would mean to hardcode
the number of elements in the lists. arbitrary length is a bit more
complicated. perhaps there's already a Lobject for it - or you could
code something fairly easily in javascript/java.

Max Patch
Copy patch and select New From Clipboard in Max.

_f

Am 16.02.2008 um 23:02 schrieb Klaas-Jan Govaart:

>
> Hello,
>
> What would be the simplest way to implement a weighted random in
> Max, comparable to the following line of SuperCollider code?
>
> [1,2,4].wchoose([0.5,0.3,0.1]);
>
> The line results in a value of 1, 2 or 3, where the weighting is:
> 1 five times more often than 4, and 2 three times more often than 4.
>
> In SC the values for the weighting don't have to add up to one,
> they are automatically normalized. They just have to be smaller
> than 1.
>
> I am aware of the possibilities of the [table] object, but am
> looking for something a bit cleaner, that can use floats et cetera.
> Ideally an object that would take two lists (values/weightings) and
> output a value when banged.
>
> Any thoughts would be appreciated.
>
> Regards,
> Klaas-Jan

#|
fredrikolofsson.com klippav.org musicalfieldsforever.com
|#

kjg's icon

Quote: f wrote on Sun, 17 February 2008 00:18
----------------------------------------------------
> good example of where syntax shines and patching gets ugly.

yes, isn't it. Wouldn't it be great to have an object like expr in which you could dump a line of SC code in cases like this?

SCexpr is on my wishlist for Max 5.1 ;)

> (although you're wrong about the auto normalisation - that'd be
> [1,2,4].wchoose([0.5,0.3,0.1].normalizeSum); to be really picky)

Oops! Of course, you are right. I just remembered it was very easy, but of course not "autocomplete_my_thought mode = 1" easy.

> simplest i don't know, but one way to do it would be with chained
> [split] or [if] statements. of course this would mean to hardcode
> the number of elements in the lists. arbitrary length is a bit more
> complicated. perhaps there's already a Lobject for it - or you could
> code something fairly easily in javascript/java.

Thanks for the example, but this is already getting "uglier" than I was hoping for.
Especially the hardcoded length of the list I don't like. Could be scripted I guess, but it would be hardly elegant.

Thanks again!
Klaas-Jan

Gary Lee Nelson's icon

Use quantile function with table object

Max Patch
Copy patch and select New From Clipboard in Max.

On 2/16/08 5:02 PM, "Klaas-Jan Govaart" wrote:

>
> Hello,
>
> What would be the simplest way to implement a weighted random in Max,
> comparable to the following line of SuperCollider code?
>
> [1,2,4].wchoose([0.5,0.3,0.1]);
>
> The line results in a value of 1, 2 or 3, where the weighting is:
> 1 five times more often than 4, and 2 three times more often than 4.
>
> In SC the values for the weighting don't have to add up to one, they are
> automatically normalized. They just have to be smaller than 1.
>
> I am aware of the possibilities of the [table] object, but am looking for
> something a bit cleaner, that can use floats et cetera. Ideally an object that
> would take two lists (values/weightings) and output a value when banged.
>
> Any thoughts would be appreciated.
>
> Regards,
> Klaas-Jan
>
>
>
>

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

Gary Lee Nelson's icon

Weights smaller than 1. & two lists

Max Patch
Copy patch and select New From Clipboard in Max.

On 2/16/08 5:02 PM, "Klaas-Jan Govaart" wrote:

>
> Hello,
>
> What would be the simplest way to implement a weighted random in Max,
> comparable to the following line of SuperCollider code?
>
> [1,2,4].wchoose([0.5,0.3,0.1]);
>
> The line results in a value of 1, 2 or 3, where the weighting is:
> 1 five times more often than 4, and 2 three times more often than 4.
>
> In SC the values for the weighting don't have to add up to one, they are
> automatically normalized. They just have to be smaller than 1.
>
> I am aware of the possibilities of the [table] object, but am looking for
> something a bit cleaner, that can use floats et cetera. Ideally an object that
> would take two lists (values/weightings) and output a value when banged.
>
> Any thoughts would be appreciated.
>
> Regards,
> Klaas-Jan
>
>
>
>

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

kjg's icon

Thank you for your input Gary, but I'm still looking for something other than table though. It just can't be use with floats, really.
In this patch I tried inputting some floats, trying to get them back out with only two decimal points of resolution. Quickly hitting the table limit of 16383 though, of course..

Any thoughts on what I could use besides table?

Max Patch
Copy patch and select New From Clipboard in Max.

LoneMonad aka don malone's icon

here is one where you can change the weights automatically

Max Patch
Copy patch and select New From Clipboard in Max.

save this one as flux in patches

Max Patch
Copy patch and select New From Clipboard in Max.

flux help window:

Gary Lee Nelson's icon

Sorry for the misunderstanding. Your original message seemed to indicate
that you were selecting only from ints.

Try this urn-like algorithm.

Build a list that has n copies of each value where n represents the weight.
1 5 2 3 4 1 would become 1 1 1 1 1 2 2 2 4
Scramble that list to something like 1 2 1 2 2 1 1 4
Cycle through the scrambled list taking and removing each item in turn.
When the list is empty, refill it and scramble again.

On 2/17/08 12:12 AM, "Klaas-Jan Govaart" wrote:

Max Patch
Copy patch and select New From Clipboard in Max.

>
> Thank you for your input Gary, but I'm still looking for something other than
> table though. It just can't be use with floats, really.
> In this patch I tried inputting some floats, trying to get them back out with
> only two decimal points of resolution. Quickly hitting the table limit of
> 16383 though, of course..
>
> Any thoughts on what I could use besides table?
>
>

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

f's icon

basic javascript solution...

///////save as wchoose.js

// /f0 080217

//weights should add up to one for it to function properly
//both lists (weights and values) should be equal in length

inlets= 2;
setinletassist(0, "bang, list");
setinletassist(1, "weights");
setoutletassist(0, "the chosen one");
var wlist= new Array();    //weights
var vlist= new Array();    //values

function list() {
    if(inlet==0) {
        vlist= arguments;
    } else {
        wlist= arguments;
    }
}
function bang() {
    var i, sum= 0;
    var r= Math.random();
    for(i= 0; i
        sum= sum+wlist[i];
        if(r
            outlet(0, vlist[i]);
            i= vlist.length;
        }
    }
}

///////test patch

Max Patch
Copy patch and select New From Clipboard in Max.

Am 17.02.2008 um 16:13 schrieb Gary Lee Nelson:

> Sorry for the misunderstanding. Your original message seemed to
> indicate
> that you were selecting only from ints.
>
> Try this urn-like algorithm.
>
> Build a list that has n copies of each value where n represents the
> weight.
> 1 5 2 3 4 1 would become 1 1 1 1 1 2 2 2 4
> Scramble that list to something like 1 2 1 2 2 1 1 4
> Cycle through the scrambled list taking and removing each item in
> turn.
> When the list is empty, refill it and scramble again.
>
>
> On 2/17/08 12:12 AM, "Klaas-Jan Govaart" wrote:
>
>>
>> Thank you for your input Gary, but I'm still looking for something
>> other than
>> table though. It just can't be use with floats, really.
>> In this patch I tried inputting some floats, trying to get them
>> back out with
>> only two decimal points of resolution. Quickly hitting the table
>> limit of
>> 16383 though, of course..
>>
>> Any thoughts on what I could use besides table?
>>
>> #P window setfont "Sans Serif" 9.;
>> #P window linecount 1;
>> #P newex 143 426 32 196617 print;
>> #P newex 143 403 41 196617 * 0.01;
>> #P button 143 52 15 0;
>> #P newex 143 137 101 196617 vexpr int($f1*100);
>> #P message 222 157 67 196617 0.5 0.3 0.1;
>> #P message 143 119 151 196617 21.234501 432.345612 7654.566895;
>> #P newex 143 180 89 196617 mxj list.Multiplex;
>> #P newex 203 337 117 196617 expr .5+$f1*1000000.;
>> #P newex 143 294 70 196617 unpack 0 0.;
>> #P button 45 119 15 0;
>> #P newex 45 234 44 196617 uzi 100;
>> #P newex 143 235 55 196617 zl group 2;
>> #P newex 143 207 99 196617 t l clear;
>> #P button 101 234 15 0;
>> #N vtable 16383 20 74 230 241 1 100000 weights;
>> #P newobj 143 363 70 196617 table weights;
>> #P window linecount 2;
>> #P comment 17 363 121 196617 weights store at location
>> corresponding to value;
>> #P window linecount 3;
>> #P comment 57 176 82 196617 100 bangs for table quantil function;
>> #P connect 7 0 6 0;
>> #P connect 14 0 11 0;
>> #P connect 11 0 13 0;
>> #P fasten 13 0 10 0 148 156;
>> #P connect 10 0 4 0;
>> #P connect 4 0 5 0;
>> #P connect 5 0 8 0;
>> #P fasten 6 0 2 0 50 354 148 354;
>> #P fasten 3 0 2 0 106 348 148 348;
>> #P fasten 4 1 2 0 237 321 148 321;
>> #P connect 8 0 2 0;
>> #P connect 2 0 15 0;
>> #P connect 15 0 16 0;
>> #P connect 8 1 9 0;
>> #P connect 9 0 2 1;
>> #P connect 14 0 12 0;
>> #P connect 12 0 10 1;
>
>
> Cheers
> Gary Lee Nelson
> Oberlin College
> www.timara.oberlin.edu/GaryLeeNelson
>
>
>

#|
fredrikolofsson.com klippav.org musicalfieldsforever.com
|#

Gary Lee Nelson's icon
Gary Lee Nelson's icon

Does wchoose.js do the job for you?

On 2/17/08 12:12 AM, "Klaas-Jan Govaart" wrote:

> Thank you for your input Gary, but I'm still looking for something other than
> table though. It just can't be use with floats, really.
> In this patch I tried inputting some floats, trying to get them back out with
> only two decimal points of resolution. Quickly hitting the table limit of
> 16383 though, of course..

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

kjg's icon

Quote: Gary Lee Nelson wrote on Mon, 18 February 2008 22:22
----------------------------------------------------
> Does wchoose.js do the job for you?
>

Thank you for your help Gary, and you too, Fredrik.

The scripts are looking good - definitely seems like the way to go in this case. Unfortunately, it's not working yet. js spits out a few syntax error messages and I haven't had any time really to go into it.
I should have some time later this week to find and eradicate these gremlins so I will report back then.

Regards,
Klaas-Jan

Gary Lee Nelson's icon

Mine is working fine as is Frederik's original java version. One problem I
have had occasionally is that my mailer breaks up long lines of text. This
can create errors when you paste the text into mxj.

On 2/18/08 9:29 PM, "Klaas-Jan Govaart" wrote:

>
> Quote: Gary Lee Nelson wrote on Mon, 18 February 2008 22:22
> ----------------------------------------------------
>> Does wchoose.js do the job for you?
>>
>
> Thank you for your help Gary, and you too, Fredrik.
>
> The scripts are looking good - definitely seems like the way to go in this
> case. Unfortunately, it's not working yet. js spits out a few syntax error
> messages and I haven't had any time really to go into it.
> I should have some time later this week to find and eradicate these gremlins
> so I will report back later.
>
> Regards,
> Klaas-Jan

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

kjg's icon

Quote: Gary Lee Nelson wrote on Tue, 19 February 2008 14:36
----------------------------------------------------
> Mine is working fine as is Frederik's original java version. One problem I
> have had occasionally is that my mailer breaks up long lines of text. This
> can create errors when you paste the text into mxj.

in that case, it would be great if you could post the .js file as a zipped attachment, so the formatting stays intact.

and when you are referring to [mxj], you mean [js], right?

thank you,
kjg

Anthony Palomba's icon

I am trying to test this js code as well. It looks like
some of the lines got truncated. Gary, can you re-paste the
source.

Gary Lee Nelson's icon

Here is again. All the lines are very short. I broke the longer comment
into two lines.

///////save as wchoose.js

// /f0 080217

//both lists (weights and values)
//should be equal in length

inlets= 2;
setinletassist(0, "bang, list");
setinletassist(1, "weights");
setoutletassist(0, "the chosen one");
var wlist= new Array(); //weights
var vlist= new Array(); //values

function list() {
var i, sum= 0;
if(inlet==0) {
vlist= arguments;
} else {
wlist= arguments;
// get sum of all weights
for(i= 0; i
sum= sum+wlist[i];
}
// scale weights to total 1.0
sum= 1.0/sum;
for(i= 0; i
wlist[i]= sum*wlist[i];
}
}
}
function bang() {
var i, sum= 0;
var r= Math.random();
for(i= 0; i
sum= sum+wlist[i];
if(r
outlet(0, vlist[i]);
i= vlist.length;
}
}
}

On 2/19/08 11:16 AM, "Anthony Palomba" wrote:

>
> I am trying to test this js code as well. It looks like
> some of the lines got truncated. Gary, can you re-paste the
> source.

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

kjg's icon

Same errors. Could you go check what's the difference between what you are emailing and how it actually ends up on the forums?

or just post a zip of the js?

Quote: Gary Lee Nelson wrote on Tue, 19 February 2008 20:55
----------------------------------------------------
> Here is again. All the lines are very short. I broke the longer comment
> into two lines.
>

kjg's icon

Quote: kjg wrote on Tue, 19 February 2008 21:16
----------------------------------------------------
> Same errors. Could you go check what's the difference between what you are emailing and how it actually ends up on the forums?
>
> or just post a zip of the js?
>

actually, you could probably see whats going wrong by looking at this - probably more convenient for you:

///////save as wchoose.js

// /f0 080217

//both lists (weights and values)
//should be equal in length

inlets= 2;
setinletassist(0, "bang, list");
setinletassist(1, "weights");
setoutletassist(0, "the chosen one");
var wlist= new Array(); //weights
var vlist= new Array(); //values

function list() {
var i, sum= 0;
if(inlet==0) {
vlist= arguments;
} else {
wlist= arguments;
// get sum of all weights
for(i= 0; i
sum= sum+wlist[i];
}
// scale weights to total 1.0
sum= 1.0/sum;
for(i= 0; i
wlist[i]= sum*wlist[i];
}
}
}
function bang() {
var i, sum= 0;
var r= Math.random();
for(i= 0; i
sum= sum+wlist[i];
if(r
outlet(0, vlist[i]);
i= vlist.length;
}
}
}

Gary Lee Nelson's icon

That looks like a mailer error.

On 2/19/08 3:20 PM, "Klaas-Jan Govaart" wrote:

>
> Quote: kjg wrote on Tue, 19 February 2008 21:16
> ----------------------------------------------------
>> Same errors. Could you go check what's the difference between what you are
>> emailing and how it actually ends up on the forums?
>>
>> or just post a zip of the js?
>>
>
> actually, you could probably see whats going wrong by looking at this -
> probably more convenient for you:
>
> ///////save as wchoose.js
>
> // /f0 080217
>
> //both lists (weights and values)
> //should be equal in length
>
> inlets= 2;
> setinletassist(0, "bang, list");
> setinletassist(1, "weights");
> setoutletassist(0, "the chosen one");
> var wlist= new Array(); //weights
> var vlist= new Array(); //values
>
> function list() {
> var i, sum= 0;
> if(inlet==0) {
> vlist= arguments;
> } else {
> wlist= arguments;
> // get sum of all weights
> for(i= 0; i
> sum= sum+wlist[i];
> }
> // scale weights to total 1.0
> sum= 1.0/sum;
> for(i= 0; i
> wlist[i]= sum*wlist[i];
> }
> }
> }
> function bang() {
> var i, sum= 0;
> var r= Math.random();
> for(i= 0; i
> sum= sum+wlist[i];
> if(r
> outlet(0, vlist[i]);
> i= vlist.length;
> }
> }
> }

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

f's icon

it's the forum that eats parts of the code. it thinks those greater/less-than are html tags or something. looks allright on the mailinglist. (another reason to use the list).

here's gary's version again but this time posted from the forum and with code tags added. looks ok in the preview at least. sorry for the mess.
_f

[code]

///////save as wchoose.js

// /f0 080217

//weights should add up to one for it to function properly
//both lists (weights and values) should be equal in length

inlets= 2;
setinletassist(0, "bang, list");
setinletassist(1, "weights");
setoutletassist(0, "the chosen one");
var wlist= new Array(); //weights
var vlist= new Array(); //values

function list() {
var i, sum= 0;
if(inlet==0) {
vlist= arguments;
} else {
wlist= arguments;
// get sum of all weights
for(i= 0; i
sum= sum+wlist[i];
}
// scale weights to total 1.0
sum= 1.0/sum;
for(i= 0; i
wlist[i]= sum*wlist[i];
}
}
}
function bang() {
var i, sum= 0;
var r= Math.random();
for(i= 0; i
sum= sum+wlist[i];
if(r
outlet(0, vlist[i]);
i= vlist.length;
}
}
}

[code]

Gary Lee Nelson's icon

Just posted the .zip file for this in the Forum with this subject

weighted random a la SC "[1,2,4].wchoose([0.5,0.3,0.1]) [message #130123]

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

Gary Lee Nelson's icon

I put a .zip in the forum.

On 2/19/08 4:06 PM, "f" wrote:

>
> it's the forum that eats parts of the code. it thinks those greater/less-than
> are html tags or something. looks allright on the mailinglist. (another
> reason to use the list).
>
> here's gary's version again but this time posted from the forum and with code
> tags added. looks ok in the preview at least. sorry for the mess.
> _f
>
>
> [code]
>
> ///////save as wchoose.js
>
> // /f0 080217
>
> //weights should add up to one for it to function properly
> //both lists (weights and values) should be equal in length
>
> inlets= 2;
> setinletassist(0, "bang, list");
> setinletassist(1, "weights");
> setoutletassist(0, "the chosen one");
> var wlist= new Array(); //weights
> var vlist= new Array(); //values
>
> function list() {
> var i, sum= 0;
> if(inlet==0) {
> vlist= arguments;
> } else {
> wlist= arguments;
> // get sum of all weights
> for(i= 0; i
> sum= sum+wlist[i];
> }
> // scale weights to total 1.0
> sum= 1.0/sum;
> for(i= 0; i
> wlist[i]= sum*wlist[i];
> }
> }
> }
> function bang() {
> var i, sum= 0;
> var r= Math.random();
> for(i= 0; i
> sum= sum+wlist[i];
> if(r
> outlet(0, vlist[i]);
> i= vlist.length;
> }
> }
> }
>
> [code]
>

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

Peter Castine's icon

Actually the simplest way would be with lp.ernie from Litter Power.

The patch below generates the output that follows.

Ernie is part of Litter Pro. I'll attach a screen shot just so you can see how simple the patch is.

Best,
Peter

Max Patch
Copy patch and select New From Clipboard in Max.

----

----

print: 1
print: 0
print: 1
print: 0
print: 0
print: 0
print: 1
print: 1
print: 1
print: 0
print: 1
print: 1
print: 2
print: 0
print: 0
print: 0
print: 0
print: 0
print: 1
print: 0
print: 0
print: 0
print: 1
print: 1
print: 2
print: 2

kjg's icon

Quote: Peter Castine wrote on Wed, 20 February 2008 17:54
----------------------------------------------------
> Actually the simplest way would be with lp.ernie from Litter Power.

But that would be the same functionality as [table], right Peter?
What about floats?

I should have mentioned it in the subject line, I know. Not just in the message (quoting myself):

"I am aware of the possibilities of the [table] object, but am looking for something a bit cleaner, that can use floats et cetera. Ideally an object that would take two lists (values/weightings) and output a value when banged."

So how about:
[f0,f1 ... f100].wchoose([0.5, 0.3 ... 0.1].normalizeSum);

Is this possible with litter too? just asking this out of curiosity by the way - the wchoose.js javascript supplied by Fredrik and Gary is doing a great job.

As are the litter pro objects, by the way :)

Thanks in advance.

Regards,
Klaas-Jan

Gary Lee Nelson's icon

This is exactly what wchoose.js does.

On 2/20/08 7:55 PM, "Klaas-Jan Govaart" wrote:

> So how about:
> [f0,f1 ... f100].wchoose([0.5, 0.3 ... 0.1].normalizeSum);

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

Gary Lee Nelson's icon

Yes but the interface is much simpler. I use lp.ernie to generate indices
to a list of values that can include floats. The output of lp.ernie goes to
zl mth to make selections from the list.

On 2/20/08 7:55 PM, "Klaas-Jan Govaart" wrote:

> But that would be the same functionality as [table], right Peter?
> What about floats?

Cheers
Gary Lee Nelson
Oberlin College
www.timara.oberlin.edu/GaryLeeNelson

kjg's icon

Yes I know. But I was asking out of curiosity if weighted random with floats could be done with Litter objects.

regards,
kjg

Quote: Gary Lee Nelson wrote on Thu, 21 February 2008 14:05
----------------------------------------------------
> This is exactly what wchoose.js does.
>
>
> On 2/20/08 7:55 PM, "Klaas-Jan Govaart" wrote:
>
> > So how about:
> > [f0,f1 ... f100].wchoose([0.5, 0.3 ... 0.1].normalizeSum);
>

kjg's icon

Ok, then that would be the way I guess. Send lp.ernie the list of int weights, and use its output to make a selection out of a list of float values.

Thank you!
Klaas-Jan

Quote: Gary Lee Nelson wrote on Thu, 21 February 2008 14:08
----------------------------------------------------
> Yes but the interface is much simpler. I use lp.ernie to generate indices
> to a list of values that can include floats. The output of lp.ernie goes to
> zl mth to make selections from the list.
>
>
> On 2/20/08 7:55 PM, "Klaas-Jan Govaart" wrote:
>
> > But that would be the same functionality as [table], right Peter?
> > What about floats?
>

>
>
----------------------------------------------------

Peter Castine's icon

Lp.ernie is Litter Pro. Which, btw, means that it supports UB (see the announcement from a week or so ago).

Lp.ernie will support up to 4095 different values, each of which can have a different weight.

To that extent it's like table. The difference is that it is implementing the urn-with-balls-of-different-colors model (more like urn). The difference to urn is that ernie can have arbitrary numbers of balls of each color. With urn you have exactly one ball of the color 'zero', one ball of the color 'one', etc.

This sort of thing (and the SC object you originally described, at least as I understood it) are inherently discrete models. Which means integers. If you want to scale integers to discrete float values, that's up to you, and you'd use lp.scampf or similar for that purpose. But as a rule, for float output you want a continuous model.

It sounds like you're possibly looking for a triangular distribution model. That would be lp.linnie. (Starter Pack). Check out Wikipedia for nice pictures . I really need to find a way to link my help files to Wikipedia, they describe so much of this distribution stuff very nicely.

-- P.

kjg's icon

Thank you for the reply Peter.

I have Litter Pro, and I was curious if there was some functionality in there I didn't know of yet, like an object that just takes two list, one of states (float or int) and one of weights (float or int), and outputs a weighted choice when banged. I didn't realize before that it just a choice between discrete states and that you actually have to generate a weighted
index to have the solution, and then use the index to select from the list of ints, floats or symbols.

Thanks for providing me with this insight Peter and Gary.

The wchoose.js script and the lp.ernie + zl mth solution both provide a way to do what I wanted.

Like you said, a discrete model - a random choice out of a list of distinct states:
[1.52, 3.87, 5.26].choose;

This is like urn, except with float "colors" for each ball. The same things could be done with urn combined with zl mth.

Or a weighted random choice:
[1.52, 3.87, 5.26].wchoose([0.05, 0.8, 0.15]);

This could be done with table or with lp.ernie combined with zl mth, provided the weights are converted to ints[vexpr int($f1*100)]?

Or a weighted random choice where the weights don't add up to one:
[1.52, 3.87, 5.26].wchoose([0.01, 0.5, 0.2].normalizeSum);

The urn like models (table/lp.ernie + zl mth) don't need the weights to add up to one so again these could be used here too.

The wchoose.js script kindly provided by Fredrik Olofsson and Gary Lee Nelson, can deals with all situations.

Peter Castine's icon

I think for your floats I would simply send ernie's output (or table's) through a coll that looked like

0, 1.52;
1, 3.87;
2, 5.26;

Sorry, of course you have Litter Pro, Klaas-Jan. I didn't immediately recognize your name from your initials.

BTW, those reading this thread who don't have Litter Pro may be interested in

kjg's icon

Quote: Peter Castine wrote on Thu, 21 February 2008 18:32
----------------------------------------------------
> I think for your floats I would simply send ernie's output (or table's) through a coll that looked like
>
> 0, 1.52;
> 1, 3.87;
> 2, 5.26;

It has dawned on me now that it is about generating indexes. From there, there's quite a few ways I guess. Coll makes a lot of sense too, indeed.

Btw, I'm using lp.ppp~ at the moment and I was trying to temporarily stop it by sending it a 0. in its left input. After that, it doesn't come back on anymore. I have to reinstantiate the object. Is this normal behavior?

It would be great to stop it in some way by sending it a message in its left input. Float zero was what I tried first (zero clicks per second), but a on/off switching behavior using ints 1/0 would be equally good.
For now I just did it with a gate of course.

Anyway, the dying after a float 0 - is that expected behavior?

Regards,
Klaas-Jan

Peter Castine's icon

Interesting quirk. But you don't need to reinstantiate the object, you just need to wait a while.

You basically have the same effect as when you set metro to an incredibly high value (say 3600000): when you change it back to a quicker value, you still have to wait an hour for the next bang and then the new value takes effect. Lp.ppp~ is similar.

It's not just zero, if you set density to, say, 0.01 pops/second and then change it to a higher density, the inter-pop time calculated while density was 0.01pps could be 100" (or longer, since the pops have a random distribution).

There are ways to implement a more immediate response when density changes from hyper-sparse to a more normal value, but I'm not sure which would be most appropriate for lp.ppp~. I'm also wary of changing behavior at this point in time.

What exactly were you hoping to achieve with a zero density?

LoneMonad aka don malone's icon

i have had this problem with lp.ppp~
does anyone have a workaround?

kjg's icon

Quote: Peter Castine wrote on Fri, 22 February 2008 12:21
----------------------------------------------------
> Interesting quirk. But you don't need to reinstantiate the object, you just need to wait a while.
>
> You basically have the same effect as when you set metro to an incredibly high value (say 3600000): when you change it back to a quicker value, you still have to wait an hour for the next bang and then the new value takes effect. Lp.ppp~ is similar.
>
> It's not just zero, if you set density to, say, 0.01 pops/second and then change it to a higher density, the inter-pop time calculated while density was 0.01pps could be 100" (or longer, since the pops have a random distribution).

Ah ok, that makes sense. So it is expected behaviour. Thank oyu for explaining.

>
> There are ways to implement a more immediate response when density changes from hyper-sparse to a more normal value, but I'm not sure which would be most appropriate for lp.ppp~. I'm also wary of changing behavior at this point in time.

I'll use a gate then

>
> What exactly were you hoping to achieve with a zero density?

Quoting myself:
" It would be great to stop it in some way by sending it a message in its left input. Float zero was what I tried first (zero clicks per second), but a on/off switching behavior using ints 1/0 would be equally good."

I was just trying to switch it off.. That sort of works, except that for reason you explained, it might take some time to come back later then.
I'll just use a gate or multiply the output by zero.

Thank you,
Klaas-Jan

PS: Congratulations on the release of Litter Power UB!

Peter Castine's icon

Quote: kjg wrote on Fri, 22 February 2008 14:11
----------------------------------------------------
> Quoting myself:
> " It would be great to stop it in some way by sending it a message in its left input. Float zero was what I tried first (zero clicks per second), but a on/off switching behavior using ints 1/0 would be equally good."
----------------------------------------------------

Sorry, I missed the text. It's been a long thread.

I can understand wanting to use lp.ppp~ this way, although I didn't originally conceive of the object with your semantics in mind.

I will look into making a change that would allow you to do what you want, and without breaking other uses. But in the meantime you'll need to use a gate~ or similar approach.

----------------------------------------------------
> PS: Congratulations on the release of Litter Power UB!
----------------------------------------------------

Thank you!

Stefan Tiedje's icon

Peter Castine schrieb:
> I really need to find a way to link my help files to Wikipedia, they
> describe so much of this distribution stuff very nicely.

That would be great, a message box with ; max_lauchbrowser url should do
it...

--
Stefan Tiedje------------x-------
--_____-----------|--------------
--(_|_ ----|-----|-----()-------
-- _|_)----|-----()--------------
----------()--------www.ccmix.com