Pwhite

Another popular member of the Pattern family is Pwhite. It is an equal distribution random number generator (the name comes from “white noise”). For example, Pwhite(100, 500) will get you random numbers between 100 and 500.

(
Pbind(
    \freq, Pwhite(100, 500),
    \dur, Prand([0.15, 0.25, 0.3], inf),
    \amp, 0.2,
    \legato, 0.3
).trace.play;
)

The example above also shows another helpful trick: the message trace just before play. It prints out in the Post window the values chosen for every event. Very useful for debugging or simply to understand what is going on!

Pay attention to the differences between Pwhite and Prand: they take in different arguments, and they do different things. Inside Pwhite’s parentheses you only need to provide a low and a high boundary: Pwhite(low, high). Random numbers will be chosen from within that range. Prand, on the other hand, takes in a list of items (necessarily between square brackets), and a number of repeats: Prand([list, of, items], repeats). Random items will be chosen from the list.

Play around with both and make sure you fully understand the difference.

TIP: A Pwhite with two integer numbers will generate only integers. For example, Pwhite(100, 500) will output numbers like 145, 568, 700, but not 145.6, 450.32, etc. If you want floating point numbers in your output, write Pwhite(100, 500.0). This is very useful for, say, amplitudes: if you write Pwhite(0, 1) you are only getting 0 or 1, but write Pwhite(0, 1.0) and you will get everything in between.

Try the following questions to test your new knowledge:

  1. What is the difference in output between Pwhite(0, 10) and Prand([0, 4, 1, 5, 9, 10, 2, 3], inf)?

  2. If you need a stream of integer numbers chosen randomly between 0 and 100, could you use a Prand?

  3. What is the difference in output between Pwhite(0, 3) and Prand([0, 1, 2, 3], inf)? What if you write Pwhite(0, 3.0)?

  4. Run the examples below. We use \note instead of \degree in order to play a C minor scale (which includes black keys). The list [0, 2, 3, 5, 7, 8, 11, 12] has eight numbers in it, corresponding to pitches C, D, E\(\flat\), F, G, A\(\flat\), B, C, but how many events does each example actually play? Why?

    // Pseq
    (
    Pbind(
        \note, Pseq([0, 2, 3, 5, 7, 8, 11, 12], 4),
        \dur, 0.15;
    ).play;
    )
    
    // Pseq
    (
    Pbind(
        \note, Prand([0, 2, 3, 5, 7, 8, 11, 12], 4),
        \dur, 0.15;
    ).play;
    )
    
    // Pwhite
    (
    Pbind(
        \note, Pseq([0, 2, 3, 5, 7, 8, 11, 12], 4),
        \dur, Pwhite(0.15, 0.5);
    ).play;
    )

Answers below.1



  1. a) Pwhite(0, 10) will generate any number between 0 and 10. Prand([0, 4, 1, 5, 9, 10, 2, 3], inf) will only pick from the list, which has some numbers between 0 and 10, but not all (6, 7, 8 are not there, so they will never occur in this Prand).
    b) Technically you could use a Prand if you provide a list with all numbers between 0 and 100, but it makes more sense to use a Pwhite for this task: Pwhite(0, 100).
    c) Prand([0, 1, 2, 3], inf) picks items from the list at random. Pwhite(0, 3) arrives at the same kind of output through different means: it will generate random integer numbers between 0 and 3, which ends up being the same pool of options than the Prand above. However, if you write Pwhite(0, 3.0), the output is now different: because one of the input arguments of Pwhite is written as a float (3.0), it will now output any floating point number between 1 and 3, like 0.154, 1.0, 1.45, 2.999.
    d) The first Pbind plays 32 notes (4 times the sequence of 8 notes). The second Pbind plays only 4 notes: four random choices picked from the list (remember that Prand, unlike Pseq, has no obligation to play all the notes from the list: it will simply pick as many random notes as you tell it to). The third and last Pbind plays 32 notes, like the first.