# Same random value in multiple Pbinds

Hello,

I guess that this question is very basic but I haven’t been able to find a solution. I need to create a random value that can be used in multiples Pbinds inside a Ptpar. I have tried to generate the value in a variable but each time I call the variable, it returns a new random value. Pseed is not a solution here because I need the values to change automatically in a loop.

In the example below, I need that both Pbinds always play the same note.

``````(
~note = Pxrand.new(#[43, 46, 49], inf);
Pdef(\o, Ptpar([
0, Pbind(\chan, 0, \midinote, ~note),
0, Pbind(\chan, 1, \midinote, ~note),
])).play(l).quant_(4);
)
``````

Thank you!

I can’t test right now but how about:

``````~noteStream.isNil.if{ ~noteStream = Pxrand([43, 46, 49], inf).asStream};
~note = ~noteStream.next;
Pdef(\o, Ptpar([
0, Pbind(\chan, 0, \midinote, ~note),
0, Pbind(\chan, 1, \midinote, ~note),
])).play(l).quant_(4);
``````

No, it didn’t work either. @semiquaver’s approach is possible in this way:

``````~noteStream = Pstutter(2, Pxrand([43, 46, 49], inf)).asStream;

Pdef(\o, Ptpar([
0, Pbind(\chan, 0, \midinote, ~noteStream),
0, Pbind(\chan, 1, \midinote, ~noteStream),
])).play(l).quant_(4);
``````

In the given example Pseed would be possible too:

``````p = Pseed(1000, Pxrand([43, 46, 49], inf));

Pdef(\o, Ptpar([
0, Pbind(\chan, 0, \midinote, p),
0, Pbind(\chan, 1, \midinote, p),
])).trace.play(l).quant_(4);
``````

The data sharing chapter in James’ pattern guide shows a general approach:

https://doc.sccode.org/Tutorials/A-Practical-Guide/PG_06g_Data_Sharing.html

Here a collection of further links:

1 Like

Both options work like a charm. I will check those links.

Thanks, @semiquaver and @dkmayer.

I vaguely remember, once upon a time, someone proposing a version of Pclutch that would produce a new value only when time had advanced… but I can’t find it at the moment.

That would look like this (in a class file):

``````PtimeClutch : FilterPattern {
embedInStream { |inval|
var stream = pattern.asStream;
var lastTime;
var event;
while {
event = stream.next(inval);
event.notNil
} {
while { thisThread.beats == lastTime } {
inval = event.copy.yield;
};
};
^inval
}
}
``````

And the usage would be something like this – left and right should follow the same rhythm:

``````(
var dur = PtimeClutch(Pexprand(0.25, 0.75, inf)).asStream;

p = Ppar([
Pbind(
\freq, Pexprand(200, 400, inf),
\dur, dur,
\pan, -0.8
),
Pbind(
\freq, Pexprand(400, 800, inf),
\dur, dur,
\pan, 0.8
)
]).play;
)
``````

Note, though, that both `\dur` bindings have to be to the same, identical stream object – so it isn’t exactly transparent. (This might be why the class was abandoned.)

hjh

Here it is - I use this all the time. Works especially well with @dkmayer 's `PS` patterns in his ` miSCellaneous` quark, to create global-ish envelopes and the like that many other patterns can pull from.