D♭ Tutorial: RANDOM CHOICES

In the previous tutorials we created a few very simple melodies. We will now enrich them by random variation.

Random choices

Each entity carries its own pseudo-random number generator. It can be easily accessed by @Rnd. The following example illustrates how it can manipulate the velocity, 'time' and span to produce a randomly bad interpretation of the original score.

0
C-1
Code Editor
0.000

Important: Since Rnd is bound to an entity, the random value can be only applied to that very same entity, but neither to a different entity, nor to global scope variables like melodyQ and melodyA in the previous example.

We plan to add a GLOBAL namespace with an own random generator in the future, to allow for randomizing the global scope variables.

For now, manipulation of the melody tones needs to be done int the transformer next to the other Rnd calls. The following example adds three shift options for each of three tones in the middle of the phrase, so in total 3³ = 27 variations per setence. Q and A are not distinguished by Sentence so the shift applies to both, resulting in 54 possible variations. Not all of them sound great, but that is not the goal of this example.

0
C-1
Code Editor
0.000

Important: Upper limits in the random functions are in D♭ inclusive. Note that this is a different behavior compared to the stadard Random in C#

Persistent variations

If you hit play for the previous examples you will hear the same music over and over again. Remember to hit the left-most button to request a new execution of the script which yields a new variation. Often, it is nice to enforce the same variation for all script executions. This can be achieved by manually setting the seed attribute; Compare the following three examples, execute each of them several times.

0
C-1
Code Editor
0.000
0
C-1
Code Editor
0.000
0
C-1
Code Editor
0.000
  • The first example will always sound differently. Its seed is not set, so the system implicitly uses a random value as the seed.

  • The second example sets the seed to a constant value. It follows, that the random sequence of ints generated inside of the Split is always the same, but it contains different tones.

  • The last example sets the seed inside the Split which results in each Rnd call to be performed with the same constant seed. Thus, all tones will be always the same.

  • How could you generate a sequence of constant but always different tones? For completeness, the next example shows that.

0
C-1
Code Editor
0.000

Another alternative would be to fix the seed to the split length either before or inside the Split, of course both with different effects. This is left as a small exercise for you.

Choices

The random generator also supports a choice from a set of alternatives. This way it is possible to work not only with floats and ints, but basically with any type.

The following example does not alter melody degrees, but combines whole pieces together. The number of resulting variations is lower, but the results do not suffer by mismatch sometimes created by free randomization.

0
C-1
Code Editor
0.000

Random vectors

If you ever need a vector of random values, Rnd offers functions with Vec suffix which produce a whole array of random values at once. The array length is the first argument of the function.

0
C-1
Code Editor
0.000

The next tutorial will show how to create harmonic progressions and chords.