## externals, table & random number

Sep 3, 2013 at 6:07am

# externals, table & random number

Hi,

How can i get the same behavior (inside an external) than a table that would be banged ? (the table outs a random number, but the probability of appearance of this number is weigthed by the values inside the table).

In fact the question should be : what kind of operations are done inside the table object when it’s banged.

Thanks !

#264097
Sep 3, 2013 at 7:29am

If you look up the ‘bang’ message to table in the manual (and follow where it leads you) you’ll find the answer.

#264100
Sep 4, 2013 at 1:40am
– Pasted Max Patch, click to expand. –
#264194
Sep 4, 2013 at 5:08am

Well i fact i was looking for a way to do it in C, in my external, but thanks anyway.

I should answer my question myself now, after a day of thinking here’s what i wrote :

int plussz_cherche_table(t_plussz *x){

long **data = NULL;
long i, size , sum =0, rand_nb = 0,temp = 0,result = 0,indice =0;

if (!table_get(gensym(“modes_majeur_5″), &data, &size)) {

for (i = 0; i < size; i++) {
sum = sum+(long)(*data)[i];
}

if(sum == 0){return(-1);}
else{rand_nb = rand()%(100*sum)+1;}
//indice = indice+(long)(*data)[0]*100;

for(i = 0; i<size;i++){
indice = indice+(long)(*data)[i]*100;
//post("temp = %ld indice = %ld rand = %ld",temp,indice,rand_nb);
if( temp < rand_nb && rand_nb < indice){
result = i;
break;
}
else{
temp = temp + (long)(*data)[i]*100;
}
}//for

}//if table existe
return result;

}//plussz_cherche_table

#264212
Sep 4, 2013 at 5:14am

So the table “modes_majeur_5″ is in the patcher but my object can see it.
Now this function looks for all the values in the table and picks one of them according to their probability.

If someone see something faster… let me know !

Yipee
;)

#264213
Sep 4, 2013 at 5:31am

I should answer my question myself now, after a day of thinking

Somehow I was under the impression that your question was “what kind of operations are done inside the table object when it’s banged.”

Chris pointed to the reference doc and I made a patch that illustrated what you would have found in the documentation had you looked. Specifically, when you bang a table object, it generates a random number between 0 and 32,768, multiplies it by the sum of the numbers in the table, divides the result by 2^15 and uses that number to look up the corresponding value in the table. Glad everything worked out for you. Good luck with your project.

#264217
Sep 4, 2013 at 8:05am

Well,

I’m sorry that my skills in english sometimes make my sentences look like reproaches, it is not my intention.

In fact i looked at the help without even thinking about looking in the reference.
I’ve just saw what you were talking about.
So my fault…

#264232
Sep 4, 2013 at 8:05am

and thanks !

#264233
Sep 4, 2013 at 10:12am

Hi,

A good idea is to post in the right place also ; for any C related question you should try the “dev” forum ;-)

#264252
Sep 4, 2013 at 4:34pm

I’m sorry that my skills in english sometimes make my sentences look like reproaches, it is not my intention.

Well.. no need to apologize. I suppose I did presume a certain degree of reproach but nothing major.. and I’m just as responsible for my assumptions.

re: help/reference.. Reference is really the ‘goto’ resource for detailed inquiry. Help should probably be called ‘quickstart’ or ‘examples’. The ‘see also’ section in the reference tab of the sidebar usually has a link to the tutorial related to the object selected in the patcher. Tutorials are also more informative than ‘help’ and they contain additional patchers with more in-depth examples.

#264309

You must be logged in to reply to this topic.