Here we just want to create a random block of binary numbers where we can specify the number of columns, the number of rows, and the frequency at which the numbers change.

Although there are a number of ways to approach this task, it turns out that JavaScript has a couple of built-in tools that make this pretty simple. The first key to our design is the use of JavaScript's **toString()** method. The most common use for this handy method is to simply convert numbers to strings. However, **toString()** has another function that uses an optional, not-so-well-know parameter. It turns out, that you can specify the base to use for the conversion. You're used to seeing numbers represented in base ten and this is the default mode of **toString()**, so left to its own devices, it will convert your number to a base ten string. However, it's also perfectly happy to convert your number to any other base. We'll take advantage of this feature to convert our random numbers to binary (base two).

You can see for yourself how **toString()** works by creating a text layer and entering this expression for the source text:

**(127.9).toString(2)**

You'll notice that the resulting string is "1111111", which is the binary representation for 127. You can see that **toString(2)** has rounded our number down to the nearest integer (127) and converted it to binary. Quite useful for our purposes.

Now we need to figure out how to produce a random number that, when converted to a binary string, will have a binary digit for each column. Normally, when you use **toString()** to convert a number to binary, it won't add leading zeros (which we need to fill up any otherwise empty columns on the left. Let's scale things down a little so that we can examine the problem in detail. Let's say we only wanted four columns. That means we want numbers between zero (binary "0000") and fifteen (binary "1111"). Using something like **random(16).toString(2)** works except that a number like five ends up as "101" instead of "0101". So what we need to do is generate a random number between 16 (binary "10000") and 31 (binary "11111") and then just use the last four digits. That gives us the leading zeros when we need them.

This brings us to the other half of the dynamic duo of JavaScript string manipulation methods that we're going to use, namely **substr()**. This is an enormously powerful method and if you're serious about expressions or scripting, you should dive into your favorite JavaScript reference and learn how it works (see sidebar for a simplified overview). Here we're just going to use it in the most basic way to drop the first character (digit) of our binary string. We'll do this by appending **.substr(1)** to our string of digits. This tells JavaScript to extract from our string the substring that begins with the second character and continues through the end of the string. The net result of this is that the first character gets dropped.

**
rows = 8;
cols = 16;
freq = 5;
posterizeTime(freq);
randMin = Math.pow(2,cols);
randMax = randMin*2;
s = "";
for(i =0; i < rows; i++){
s += random(randMin,randMax).toString(2).substr(1) + "\r";
}
s
**

Note that for best results, you'll want to use a mono spaced font (meaning that each character is the same width) so that the width of the display doesn't jump around. Lucida Console was used for this example.

The JavaScript string method **substr()** is used to extract a substring from a string and takes two parameters. The first parameter is the zero-based index of where to start extracting the substring. That means that "0" represents the first character, "1" represents the second character, and so on. The second (optional) parameter specifies how many characters to extract. For example, the expression ** ("abcdef").substr(1,3)** would return "bcd". The second parameter is optional, and if it is omitted, the characters from the first parameter through the end of the string are returned. So, the expression **("abcdef").substr(1)** would return "bcdef".

Notice that we're using the JavaScript method **Math.pow()** here. **Math.pow() ** takes two parameters, and raises the first parameter to power specified by the second parameter. We use it here to raise the number "2" to the 16th power (the value of **cols**). This gives us a minimum random value of 65536 which, when converted to binary, consists of a one followed by sixteen zeros.