• jon@schemawound.com

A Little Less Random

My song “Hello World. I Am Lonely Too” uses quite a few random numbers throughout the track.  While is this is good for providing infinite variations there is a drawback, I will never be able to produce an exact copy of the album version of the track.  This is unfortunate because I recorded the track as a stereo mixdown.  It is no longer possible to separate the instruments for remixes of other purposes.

If I had been a little smarter I would have seeded my random numbers.  A random seed is a number used to initialize a psuedorandom number generator.  Using the same seed will always result in the same sequence of random numbers.  This is because no number generated by a computer is ever truly random, it is always generated by an algorithm.  If you want more background here is some info on randomness within Supercollider and general information on random number generators.

To give an example of seeding a random number sequence within Supercollider I have written up the following example.  First I create a simple SynthDef.  Next I create a function that takes a random seed as a parameter and uses it to seed the random number generator used within the function.  The Pbind function utilizes random numbers for both the Prand and Pwhite.  I use the trace command to post each event to the post window.  I then call the function four times using the same seed.  In the post window you will note this causes it to spit out the same sequence of events four times.  I then call the same function four times using a different seed and similar results are produced.

(
	{
		//Create a simple SynthDef
		SynthDef(x, {|freq = 200| Out.ar(0, SinOsc.ar(freq)*EnvGen.ar(Env.perc(0.1,0.3))!2 * 0.3)}).add;
		//Wait until the SynthDef has been created on the server
		s.sync;
		//Create a function that takes a random seed
		x = {|seed = 0|
			thisThread.randSeed = seed;
			Pbind(*[
				instrument:	x, 
				note: 		Prand([2.5,5.5,3.5,4.3,4.7], 4),
				octave:		5,
				dur: 		Pwhite(0.1,1,inf)
			]).trace.play;
		};
		//Play a pattern 4 times with the same seed.  Notice the pattern repeats.
		x.(102); 2.wait;
		x.(102); 2.wait;
		x.(102); 2.wait;
		x.(102); 2.wait;
		//Switch up the seed and the pattern changes.
		x.(104); 2.wait;
		x.(104); 2.wait;
		x.(104); 2.wait;
		x.(104); 2.wait;
	}.fork
)

For future compositions I will be sure to seed my random numbers to ensure I can repeat results as needed.