Jump to content

Generating random notes: Difference between revisions

From LilyPond wiki
No edit summary
m New category
Line 26: Line 26:
[[Category:Really cool]]
[[Category:Really cool]]
[[Category:Scheme]]
[[Category:Scheme]]
[[Category:Included in the official documentation]][[Category:Snippet]]
[[Category:Included in the official documentation]]
[[Category:Snippet]]

Revision as of 23:33, 21 November 2025

This Scheme-based snippet generates random notes. Use as

\randomNotes n from to dur

to generate n random notes between pitches from and to, with duration dur.

\version "2.24"

randomNotes =
#(define-music-function (n from to dur)
   (integer? ly:pitch? ly:pitch? ly:duration?)
   (let ((from-step (ly:pitch-steps from))
         (to-step (ly:pitch-steps to)))
     (make-sequential-music
      (map (lambda (_)
             (let* ((step (+ from-step
                             (random (- to-step from-step))))
                    (pitch (ly:make-pitch 0 step 0)))
               #{ $pitch $dur #}))
           (iota n)))))

\randomNotes 24 c' g'' 8