# Drand with variable list size

There is a way to make a sequencer that sequences over an array of values. You can supply any array through the namedcontrol, as long as its smaller than the intial size of the array, which is in this case 20. If you supply a array smaller than size 20, you have to use the \size.kr() named control accordingly. The point of this is that i can set it to different arrays of different sizes while its playing. This method is taken from here https://scsynth.org/t/problems-with-variable-literal-array-and-namedcontrol/941/3

``````(
x = {
var freq, trig;
trig = Impulse.kr(1);
freq = Demand.kr(trig, 0, Dseq(Dser(\pattern.kr(100!20), \size.kr(1)), inf));
SinOsc.ar(freq) * EnvGen.kr(Env.perc(), trig);
}.play;
)

(
var pattern1 = [100, 200, 300];
x.set(\pattern, pattern1);
x.set(\size, pattern1.size);
)
``````

Now comes my question.
I want to have this exactly, but with Dseq replaced with Drand. However when i do that it just plays the array of values as a sequence. Why does this not work? Is there a way around this?

``````(
x = {
var freq, trig;
trig = Impulse.kr(1);
// here Drand is used instead of Dseq
freq = Demand.kr(trig, 0, Drand(Dser(\pattern.kr(100!20), \size.kr(1)), inf));
SinOsc.ar(freq) * EnvGen.kr(Env.perc(), trig);
}.play;
)

(
var pattern1 = [100, 200, 300];
x.set(\pattern, pattern1);
x.set(\size, pattern1.size);
)
``````

I want the functionality of the first method (with Dseq), but it should play the given array of values in a random order.

I understand now, how obvious it might be to anyone on the side, why the second method doesnt work. Drand wants a list of number or a list of ugens and the Dser was the only element in that list, it doesnt know about the insides of Dser. So ofcourse it just chose randomly out of a list of 1 elements, which was the given Dser.
The workaround i found is this, its not so beautiful but it works.

``````(
x = {
arg pat, size;
var freq, trig;
trig = Impulse.kr(1);
freq = Demand.kr(trig, 0, Dwrand(\pattern.kr(0!20), \weights.kr( ++ (0!19)), inf));
LFTri.ar(freq) * EnvGen.kr(Env.perc(0.01, 0.4), trig);
}.play;
)

(
var pattern = [100, 150, 700, 45, 70];
var complement = 20 - pattern.size;
//all values have the same weight, the tail of zeros get a weight of 0
var weights = (1/pattern.size)!pattern.size ++ (0!complement);
pattern = [pattern] ++ (0!complement);
x.set(\pattern, pattern);
x.set(\weights, weights);
)
``````

Another way using Diwhite and Select:

``````(
x = {
var freq, trig, index;
trig = Impulse.kr(1);
index = Demand.kr(trig, 0, Diwhite(0, \size.kr(1) - 1));
freq = Select.kr(index.asInteger, \pattern.kr(100!20));
0.2 * SinOsc.ar(freq) * EnvGen.kr(Env.perc(), trig);
}.play;
)

(
var pattern1 = [100, 200, 300];
x.set(\pattern, pattern1);
x.set(\size, pattern1.size);
)
``````

Best,
Paul

Oh i like this method more!