Having cut my programming teeth on Max/MSP, I’m often left wanting more from the basics in general-purpose programming languages. As evidenced by the awesome jQuery/underscore libraries, there’s a lot of gaps to fill when it comes to javascript utility functions. Although not as commonly useful as underscore’s each or map, I have a great need for non-repeating random values for the purposes of selecting from collections.

Autechre's max patch "you messy motherfucker"

Autechre's max patch aka "you messy motherfucker"

In Max, there are a large number of objects that let you scramble and remix lists of items in interesting ways. Because Max is music focused, a lot of your operations revolve around working with permanent sets. Defining a scale of possible notes doesn’t do you much good if your system is able to randomly select F# 300 times in a row. Max provides the Urn object for this purpose, a ‘grab bag’ for collections. Given a number range, urn returns a member every time it’s triggered. It’s unable to produce the same number again until all other members have been selected.

I’ve created a similar object in javascript. A single urn can contain multiple collections, each keyed by the array it’s self:

var urn = new Urn();
var items = ['mice','dogs','tigers','cats'];
urn.grab(items); //--> 'dogs'
urn.grab(items); //--> 'tigers'
urn.grab(items); //--> 'mice'
urn.grab(items); //--> 'cats'

//throw a different array into the same object
//one urn tracks all keys in parallel
var junk = ['maggie','lisa','bart'];
urn.grab(junk); //--> 'maggie'
urn.grab(junk); //--> 'bart'
urn.grab(junk); //--> 'lisa'
urn.grab(junk); //--> 'lisa'
urn.grab(junk); //--> 'maggie'
urn.grab(junk); //--> 'bart'

The only missing feature is preventing the end of one sequence being the same as the beginning of the next. Notice the two ‘lisa’s in a row in the above example. Each full round of urn grabs are managed independently.

Grab urnie.js over on the github: https://github.com/jackaperkins/urnie

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s