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

static random-number generator 'defeated' by seed in constructor #1

Open
mmurrian opened this issue Nov 16, 2021 · 5 comments
Open

Comments

@mmurrian
Copy link

Every construction of an EigenMultivariateNormal instance re-seeds the static random-number generator. This will cause all instances to reset their RNG sequence back to the same output whenever a new EigenMultivariateNormal is created. If samples() is only called once per instance, the output is always the same!

I recommend providing a static setSeed(...) function that the user can explicitly call and not setting the seed in the constructor.

@mmurrian
Copy link
Author

See pull request #2

@beniz
Copy link
Owner

beniz commented Nov 16, 2021

My understanding is that the twister seeds to a fixed seed by default, see https://www.cplusplus.com/reference/random/mt19937/ so what you need to do here is to seed differently yourself and call the constructor.

@mmurrian
Copy link
Author

mmurrian commented Nov 16, 2021

// Construct random-number generator #1
EigenMultivariateNormal mvnrnd1(mean, covar);

while(1) {
  // Construct another random-number generator
  EigenMultivariateNormal mvnrnd2(mean, covar);

  const auto sample1 = mvnrnd1.samples(1);
  const auto sample2 = mvnrnd2.samples(1);
}

In this example, sample1 will always generate the same value.

The problem isn't that mvnrnd1 and mvnrnd2 generate the same sequence (they don't). The problem is that mvnrnd1 gets stuck at the same value because mvnrnd2 is being constructed. Why should constructing one random-number generator break another one?

@beniz
Copy link
Owner

beniz commented Nov 17, 2021

Have you tried have the twister non static on line https://github.com/beniz/eigenmvn/blob/master/eigenmvn.h#L51?

@mmurrian
Copy link
Author

I fixed the bug per pull request #2. Making the twister non-static is another solution.

Given the purpose of your code, I think having a static twister is the better choice. If multiple instances of EigenMultivariateNormal are drawing from the same twister in, say, multiple, unsynchronized threads... you can potentially get true randomness out it.

But there are valid reasons to make it non-static (if you actually want a deterministic, pseudorandom output).

Either way, the current behavior is buggy.

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

No branches or pull requests

2 participants