# Randomness in a SynthDef

I’ve been looking in the help docs and books, but I can’t seem to figure out how to get random numbers of a scale within a SynthDef,

Specifically something like this:

``````Scale.yu.degreeToFreq(IRand(0,12),2640,0);
``````

I keep getting error: ERROR: Primitive ‘_BasicWrapAt’ failed.

Full code:

``````(
SynthDef(\hh, {
arg out=0, gate=1;
var sig, o1, o2, png, penv, henv, tail, env, n0, penv2, scale, rand;
rand = Rand(0,12);
scale = Scale.phrygian.degreeToFreq(rand,2640,0);
penv = EnvGen.ar(Env.perc(0.001,0.15).range(1000,20000));
penv2 = EnvGen.ar(Env.perc(0.001,0.35));
henv = EnvGen.ar(Env.new([2640,4000,2640],[0.2,0.4]));
env = EnvGen.ar(Env.perc(0.001,0.45),gate,doneAction:2);
n0 = BrownNoise.ar(1);
o1 = Pulse.ar(scale/10,0.7)!4;
o2 = Pulse.ar(Rand(2490,3491)+o1,0.5)!4*o1*0.5;
o2 = Pulse.ar(scale+o1,0.5)!4*o1*0.5;
png = BPF.ar(o2, 1000+penv, 1)*penv2*1;
tail = BPF.ar(o2+n0,henv,0.2)*penv2!2;
sig = HPF.ar(o2+png*0.6+tail, 2000);
Out.ar(out, sig*env );
)
``````

DegreeToKey | SuperCollider 3.11.1 Help should get you part of the way there.

hjh

That looks pretty cool too, but I’m not trying to convert a signal to note values, I’m just trying to chose a number from 1 to 12 randomly each time the synth is played within a certain scale.

This is a signal.

hjh

1 Like

I’m so confused.

I’m mostly just using Scale.major.degreetoFreq as an example, where I want to pass it randomly chosen numbers (1…12).

I’m trying to generate a random number each time the synth Plays, sot each time it’s built on the server.

It looks like Rand does that, and as you said that is a Signal, but degreetoFreq can’t take a signal as an input, so it fails, which would happen with any other function I try to give Rand to?

It’s ok, maybe I just need to keep reading and it will make sense…

`degreeToFreq` is a language-side calculation.

So when you say “generate a random number each time the synth Plays, so each time it’s built on the server” – if the random number exists only in the server, then it isn’t available to language-side operations.

So there are two directions you can go:

1. Generate the random number language-side, do `degreeToFreq` in the language, and pass that frequency to the synth in the argument list.

2. Or, use server-side operations to get the frequency from the server-side random number. `DegreeToKey` is server side, and it’s the hard part of `degreeToFreq`.

Both would work.

hjh