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

added linear congruential generator for streams and lists #56

wants to merge 4 commits into from

Conversation

dariosanfilippo
Copy link
Contributor

Hello, people.

If I'm not wrong, there was no random numbers generator accepting a seed in the libraries so I added this basic LCG. There are both a function to generate streams of values in the sequence, as well as a function to generate N values in the sequence in parallel (lists, so to speak).

Depending on the parameters, the sequences can have short periods, and the values are outside of the digital audio range. So I thought that maths.lib was a better place than noises.lib.

Best,
Dario

@josmithiii
Copy link
Collaborator

Looks nice to have!

If it's not going to noises.lib, then I think random-number generators in both files should have SEE ALSO comments pointing to each other.

In principle, noise should be reimplemented as a special case of lcg. However, it looks like it would be more expensive, and the current simple noise function is nice to keep for its clarity and simplicity.

@dariosanfilippo
Copy link
Contributor Author

Nice hearing from you, Julius. :-)

Sure, I can cross-reference the functions.

For completeness, perhaps we can also add the following in noises.lib for a seeded 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
};

Best,
Dario

@josmithiii
Copy link
Collaborator

josmithiii commented Oct 29, 2020 via email

@dariosanfilippo
Copy link
Contributor Author

@sletz @josmithiii does this PR need any further improvements?

Best,
Dario

@sletz
Copy link
Member

sletz commented Nov 2, 2020

Seems like a nice addition, OK for you @orlarey and @rmichon ?

@rmichon
Copy link
Contributor

rmichon commented Nov 2, 2020

Looks good to me...

@dariosanfilippo
Copy link
Contributor Author

By the way, changing the increment as I did in the noiseed function does not seem to affect the uniformity of the generated stream: I tried averaging the streams over 1e+08 samples at 48kHz SR and I get these values (test code below):

5.9520071088884344e-05, -1.3798191843195161e-05, -2.0827678791606534e-05, 1.8723615871972751e-05

import("stdfaust.lib");
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
};
mean(x) = fi.pole(1, x) / (ba.time +1);
noise1 = noiseed(12345);
noise2 = noiseed(1);
noise3 = noiseed(40);
noise4 = noiseed(2);
process = mean(noise1) , mean(noise2) , mean(noise3) , mean(noise4);

@sletz
Copy link
Member

sletz commented Dec 22, 2020

@orlarey can you have a look and comment ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants