Randomness
Randomness is frequently used in algorithmic music to introduce variation and unpredictability. However, in many compositional workflows, reproducibility is just as important. In bellplay~, there are two types of random functions:
- Unseeded: always produce different results.
Examples:
rand
,choose
,urn
. - Seeded (prefixed with
x
): produce the same result each time when a non-zero seed is set. Examples:xrand
,xchoose
,xrandom
,xscramble
,urn
(when seeded viasetseed
).
The seeded variants allow you to control randomness and make your scripts deterministic and repeatable.
rand
vs xrand
The rand
function produces a random float and is not seedable.
## will return a different result every time
print(rand(0, 1))
In contrast, xrand
is a seeded version:
## set the random seed
setseed(@seed 42);
## will return the same value every time the script is run
print(xrand(0, 1))
If setseed
is not called, the default seed for all seeded functions is 1
.
choose
vs xchoose
choose
selects elements from a list, non-deterministically:
$files = 'flute.wav' 'viola.wav' 'guitar.wav';
print(choose(@choices $files))
With xchoose
, the result becomes reproducible if a seed is set:
setseed(@seed 99);
$files = 'flute.wav' 'viola.wav' 'guitar.wav';
print(xchoose(@choices $files))
xscramble
: Seeded Shuffling
The xscramble
function randomly reorders a list, but deterministically with a seed.
setseed(@seed 7);
$items = 1 2 3 4 5;
print(xscramble(@llll $items))
urn
: Controlled Non-Repetition
The urn
function generates unique numbers without repetition. It can be seeded using setseed
, enabling deterministic but non-repeating behavior.
setseed(@seed 2025);
initurn(
@id 'myseq'
@size 4
@useseed 1
);
for $i in (0...10) do (
print(urn(@id 'myseq'))
)
Putting It All Together
The following script demonstrates how to use setseed
to control different parameters.
## set seed for deterministic random behavior (try 0 to disable it)
setseed(120);
## initialize onset time
$t = 0;
## total duration of the sequence in milliseconds
$totdur = 10000;
## define pitch class set (in symbolic pitch notation)
$chord = c5 d5 e5 g5 b5;
## generate events until reaching total duration
while $t < $totdur do (
## randomly select a pitch from the chord
$pitch = xchoose($chord);
## choose a random octave transposition (±2 octaves, in midicents)
$octave = xrandom(-2, 3) * 1200;
## randomly stereo panning (0 to 1)
$pan = xrand();
## randomly choose duration between 1 and 5 seconds
$dur = xrand(1000, 5000);
## randomly set gain (from soft to loud)
$gain = xrand(0.25, 1);
## create a buffer using ezsampler with random pitch and duration
$b = ezsampler(@pitch $pitch + $octave @duration $dur);
## transcribe the buffer with randomized parameters
$b.transcribe(
@onset $t
@pan $pan
@gain $gain
);
## increment time by a small random step to control event density
$incr = xrand(25, 300);
$t += $incr
);
## render and apply subtle reverb
render(
@play 1 @process freeverb(
@roomsize 0.7
@damp 0.8
@width 0.25
@wet 0.1
)
)
Summary
Function | Seeded? | Returns | Use Case |
---|---|---|---|
rand | ❌ | float | quick randomness |
xrand | ✅ | float | reproducible randomness |
choose | ❌ | any | random choice |
xchoose | ✅ | any | repeatable random choice |
xscramble | ✅ | list | deterministic shuffling |
urn | ✅ (via setseed ) | int | deterministic non-repeating |
Using setseed
lets you design stochastic processes that behave consistently, which is particularly useful in collaborative work, score rendering, or iterative design.