Jump to content

Generating random notes: Difference between revisions

From LilyPond wiki
m Replace version="2.24.0" with version="2.24" now that the LilyWiki extension supports auto-selecting the latest release in a stable series
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
This Scheme-based snippet generates random notes. Use as
This Scheme-based snippet generates random notes. Use as


<code>\randomNotes </code>''<code>n</code>''<code> </code>''<code>from</code>''<code> </code>''<code>to</code>''<code> </code>''<code>dur</code>''
\randomNotes <var>n</var> <var>from</var> <var>to</var> <var>dur</var>


to generate ''n'' random notes between pitches ''from'' and ''to'', with duration ''dur''.
to generate ''n'' random notes between pitches ''from'' and ''to'', with duration ''dur''.
Line 27: Line 27:
[[Category:Scheme]]
[[Category:Scheme]]
[[Category:Included in the official documentation]]
[[Category:Included in the official documentation]]
[[Category:Snippet]]

Latest revision as of 07:11, 3 December 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