Wednesday, October 14, 2026

[gjadlrnh] d12 pick a random key

or random starting note, for a piece you want to practice in all transpositions (perhaps Well Tempered Clavier).

#!perl -lw
@a=split//,<<'eof';
C
G
D
A
E
B
F# / G flat
C# / D flat
G# / A flat
D# / E flat
A# / B flat
F
eof
print$a[int rand@a];

notes ordered in circle of fifths rather than chromatically in case the following Perl negative array index trick becomes useful in the future.  sign of index indicates whether the key signature is sharps or flats; absolute value indicates number of accidentals:

$a[0] == C
$a[1] == G
$a[-1] == F

a relabeled d12 physical die is probably not a great implementation of this randomizer, as music performance or practice typically does not have a tabletop to roll dice.  ironically, a percussion table would be good for rolling dice, but percussion instruments are typically unpitched.

No comments :