Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added linear congruential generator for streams and lists #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion maths.lib
Original file line number Diff line number Diff line change
Expand Up @@ -818,4 +818,80 @@ declare avg_t19 copyright "Copyright (C) 2020 Dario Sanfilippo
<[email protected]> and
2003-2020 by Julius O. Smith III <[email protected]>";
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
<[email protected]>";
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
<[email protected]>";
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);
32 changes: 25 additions & 7 deletions noises.lib
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,41 @@ 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
//
// ```
// 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.
Expand Down