lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scheme function evluates only once


From: David Kastrup
Subject: Re: Scheme function evluates only once
Date: Thu, 23 Jan 2020 21:33:29 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Amir Teymuri <address@hidden> writes:

> Hi,
>
> this is just a nonsense exercise for me to get to know LilyPond better!
> I can't figure out why in the following the #(rnd-col) is evaluated only
> once and not upon each compilation of my ly file? The first time i
> compile this i will get the four colors displayed:
>
> (0.5 0.5 0)
> (0.5 0 1.0)
> (0.5 0 1.0)
> (0 0.5 1.0)
>
> which however doesn't change in next compilations, so that there is no
> color change. Can someone explain what am i missing?
> Thanks
>
>
> %%
> \version "2.19.83"
>
> #(define (rnd-col) (map (lambda (x) (* x 0.5)) (list (random 3) (random 3) 
> (random 3))))
>
> coloredAcc = #(define-music-function () ()
>              (newline)
>              (display (rnd-col))
>              #{              
>              \once \override Accidental.color = #(rnd-col)
>              #})
>
> \relative c'' {
>   \coloredAcc cisis
>   \coloredAcc cisis!
>   \coloredAcc cisis!
>   \coloredAcc cisis!
> }
> %%

Guile manual:

File: guile.info,  Node: Random,  Prev: Bitwise Operations,  Up: Numbers

6.6.2.14 Random Number Generation
.................................

Pseudo-random numbers are generated from a random state object, which
can be created with ‘seed->random-state’ or ‘datum->random-state’.  An
external representation (i.e. one which can written with ‘write’ and
read with ‘read’) of a random state object can be obtained via
‘random-state->datum’.  The STATE parameter to the various functions
below is optional, it defaults to the state object in the
‘*random-state*’ variable.

[...]

   Note that the initial value of ‘*random-state*’ is the same every
time Guile starts up.  Therefore, if you don’t pass a STATE parameter to
the above procedures, and you don’t set ‘*random-state*’ to
‘(seed->random-state your-seed)’, where ‘your-seed’ is something that
_isn’t_ the same every time, you’ll get the same sequence of “random”
numbers on every run.

   For example, unless the relevant source code has changed, ‘(map
random (cdr (iota 30)))’, if the first use of random numbers since Guile
started up, will always give:

     (map random (cdr (iota 19)))
     ⇒
     (0 1 1 2 2 2 1 2 6 7 10 0 5 3 12 5 5 12)

   To seed the random state in a sensible way for non-security-critical
applications, do this during initialization of your program:

     (set! *random-state* (random-state-from-platform))



-- 
David Kastrup



reply via email to

[Prev in Thread] Current Thread [Next in Thread]