RNG in C-external instances

Mar 2, 2012 at 4:44pm

RNG in C-external instances

hallo, i’m trying to generate a different random number in different instances of an external. can you give me some advise

i’m using the normal rand(). Is the sdk offering something else/better?

the number is generated at object initialization. could it happen, if i use time as seed, that two instances generate the same number because initialization of both objects happens faster than 1ms?? if so, what can give me faster seeds?

Thanbks O.

Mar 2, 2012 at 5:18pm


AFAIK class is loaded once (“main” method of your external) ; so isn’t it possible to seed the RNG there ?

Mar 2, 2012 at 5:48pm

hey, not sure if i get what you mean. “class is loaded once”?
i’m using the “new” function to generate the number. it doesn’t matter where i seed it. Interessting for me is what happens if i load a patch with two instances of the ext, how can i make sure both instances are generating a different random number when they are created.
with different seeds, right? how do i get different seeds so fast?

i don’t want to do intance to instance messages for this simple task.

tell me if it’s still hard to understand my question.

Mar 2, 2012 at 6:06pm


for example in case of a [flop] external you can seed the RNG there …

int main (void)
t_class	*c = NULL;

c = class_new ("flop", (method)flop_new, (method)flop_free, (long)(sizeof(t_flop)), 0L, A_GIMME, 0);

class_addmethod	(c, (method)flop_assist, "assist", A_CANT, 0);
class_addmethod (c, (method)flop_bang,	 "bang", 0);

srand ((unsigned int)time(NULL));

class_register (CLASS_BOX, c);

flop_class = c;

return 0;

… instead of in the flop_new() function as the main() method is called only one time (for the FIRST created [flop] object). You can still use rand() function in flop_new() to get your random number. Problem occurs if you seed the RNG in flop_new() with time if instantiation occurs in the same millisecond ; but as you instantiate it only the first time it will be OK.

Is it clear ? I don’t think so ;-)

Mar 2, 2012 at 6:19pm

do you mean main() is only called once even if a second and third instance is created? so that rand_s in instance 2 will create random number 2 of the seed? i’ll try that out.
thanks for your help so far..

Mar 2, 2012 at 6:52pm


that’s exactly what i mean ;-)

Mar 2, 2012 at 7:39pm

cool, it works. and learned something..
thanks, bye


You must be logged in to reply to this topic.