From 2444a6fb2a432251fb90b6268b424835f0739d30 Mon Sep 17 00:00:00 2001 From: Dario Sanfilippo Date: Tue, 27 Oct 2020 14:01:30 +0100 Subject: [PATCH 1/3] added linear congruential generator for streams and lists --- maths.lib | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/maths.lib b/maths.lib index bb0b66f4..ea088875 100644 --- a/maths.lib +++ b/maths.lib @@ -818,4 +818,78 @@ 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. +// +// #### 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. +// +// #### Usage +// +// ```` +// lcg_par(N, M, A, C, S) : _ +// ```` +// +// 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); From b4cd054a4ecdffd820765a0d320ceb09cecedb0b Mon Sep 17 00:00:00 2001 From: Dario Sanfilippo Date: Thu, 29 Oct 2020 11:15:32 +0100 Subject: [PATCH 2/3] added cross-reference for lcg and noise functions --- maths.lib | 8 +++++--- noises.lib | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/maths.lib b/maths.lib index ea088875..307ec000 100644 --- a/maths.lib +++ b/maths.lib @@ -825,6 +825,7 @@ avg_t19(period, x) = fi.lpt19(period, x); // 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 // @@ -860,11 +861,12 @@ lcg(M, A, C, S) = ((+ (S - S') * A + C) % M) // 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) : _ +// lcg_par(N, M, A, C, S) : si.bus(N) // ```` // // Where: @@ -891,5 +893,5 @@ 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); +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..b3eded18 100644 --- a/noises.lib +++ b/noises.lib @@ -53,6 +53,7 @@ closed source license or any other license if you decide so. //-------`(no.)noise`---------- // White noise generator (outputs random number between -1 and 1). // `Noise` is a standard Faust function. +// See also the lcg and lcg_par functions in maths.lib. // // #### Usage // From 445e8d793cd6c294819ef405ccdcb910b9457fea Mon Sep 17 00:00:00 2001 From: Dario Sanfilippo Date: Thu, 29 Oct 2020 11:25:16 +0100 Subject: [PATCH 3/3] added seed in noise generator --- noises.lib | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/noises.lib b/noises.lib index b3eded18..36d4748c 100644 --- a/noises.lib +++ b/noises.lib @@ -49,9 +49,31 @@ 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. // @@ -61,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.