diff --git a/maths.lib b/maths.lib index bb0b66f4..307ec000 100644 --- a/maths.lib +++ b/maths.lib @@ -818,4 +818,80 @@ declare avg_t19 copyright "Copyright (C) 2020 Dario Sanfilippo and 2003-2020 by Julius O. Smith III "; declare avg_t19 license "MIT-style STK-4.3 license"; -avg_t19(period, x) = fi.lpt19(period, x); +avg_t19(period, x) = fi.lpt19(period, x); + + +//----------(ma.)lcg------------------------------------------------------------ +// Linear congruential generator for streams of integer values based on +// the equation: y[n] = (A * y[n - 1] + C) % M. +// See https://en.wikipedia.org/wiki/Linear_congruential_generator. +// See also noises.lib for noise generators based on the same mechanism. +// +// #### Usage +// +// ```` +// lcg(M, A, C, S) : _ +// ```` +// +// Where: +// +// * M is the divisor in the modulo operation. +// * A is the multiplier. +// * C is the offset. +// * S is the seed. +// +// #### Reference: +// +// L’ecuyer, P. (1999). Tables of linear congruential generators of different +// sizes and good lattice structure. Mathematics of Computation, 68(225), +// 249-260. +// +// Steele, G., & Vigna, S. (2020). Computationally easy, spectrally good +// multipliers for congruential pseudorandom number generators. arXiv +// preprint arXiv:2001.05304. +//------------------------------------------------------------------------------ +declare lcg author "Dario Sanfilippo"; +declare lcg copyright "Copyright (C) 2020 Dario Sanfilippo + "; +declare lcg license "GPL v3 license"; +lcg(M, A, C, S) = ((+ (S - S') * A + C) % M) + ~ _; + +//----------(ma.)lcg_par-------------------------------------------------------- +// Linear congruential generator for lists of integer values based on +// the equation: y[n] = (A * y[n - 1] + C) % M. +// See https://en.wikipedia.org/wiki/Linear_congruential_generator. +// See also noises.lib for noise generators based on the same mechanism. +// +// #### Usage +// +// ```` +// lcg_par(N, M, A, C, S) : si.bus(N) +// ```` +// +// Where: +// +// * N is the number of values of the sequence in parallel +// (known at compile-time). +// * M is the divisor in the modulo operation. +// * A is the multiplier. +// * C is the offset. +// * S is the seed. +// +// #### Reference: +// +// L’ecuyer, P. (1999). Tables of linear congruential generators of different +// sizes and good lattice structure. Mathematics of Computation, 68(225), +// 249-260. +// +// Steele, G., & Vigna, S. (2020). Computationally easy, spectrally good +// multipliers for congruential pseudorandom number generators. arXiv +// preprint arXiv:2001.05304. +//------------------------------------------------------------------------------ +declare lcg_par author "Dario Sanfilippo"; +declare lcg_par copyright "Copyright (C) 2020 Dario Sanfilippo + "; +declare lcg_par license "GPL v3 license"; +lcg_par(1, M, A, C, S) = (A * S + C) % M; +lcg_par(N, M, A, C, S) = (A * S + C) % M , + lcg_par(N - 1, M, A, C, (A * S + C) % M); diff --git a/noises.lib b/noises.lib index c68f7eb7..36d4748c 100644 --- a/noises.lib +++ b/noises.lib @@ -49,10 +49,33 @@ to be LGPL or GPL. For example you are free to choose a commercial or closed source license or any other license if you decide so. ************************************************************************ ************************************************************************/ +//----------`(no.)noiseed`------------------------------------------------------ +// White noise generator with seed (outputs random numbers between -1 and 1). +// `Noiseed` is a standard Faust function. +// See also the lcg and lcg_par functions in maths.lib. +// +// #### Usage +// +// ``` +// noiseed(S) : _ +// ``` +// +// Where: +// +// * S is the seed of the noise generator. +//------------------------------------------------------------------------------ +noiseed(S) = random / RANDMAX + with { + mask = 4294967295; // 2^32-1 + random = +(S) + ~ *(1103515245) & mask; // "linear congruential" + RANDMAX = 2147483647.0; // = 2^31-1 = MAX_SIGNED_INT in 32 bits + }; //-------`(no.)noise`---------- -// White noise generator (outputs random number between -1 and 1). +// White noise generator (outputs random numbers between -1 and 1). // `Noise` is a standard Faust function. +// See also the lcg and lcg_par functions in maths.lib. // // #### Usage // @@ -60,12 +83,7 @@ closed source license or any other license if you decide so. // noise : _ // ``` //------------------------ -noise = random / RANDMAX -with{ - mask = 4294967295; // 2^32-1 - random = +(12345) ~ *(1103515245) & mask; // "linear congruential" - RANDMAX = 2147483647.0; // = 2^31-1 = MAX_SIGNED_INT in 32 bits -}; +noise = noiseed(12345); //---------------------`(no.)multirandom`-------------------------- // Generates multiple decorrelated random numbers in parallel.