dpilger26 / NumCpp

C++ implementation of the Python Numpy library
https://dpilger26.github.io/NumCpp
MIT License
3.51k stars 548 forks source link

The random::seed behavior of NumCpp is inconsistent with that of numpy #171

Closed barrierye closed 1 year ago

barrierye commented 1 year ago

Describe the bug I tried to migrate a piece of code from numpy to NumCpp, but the random::seed behavior of NumCpp is inconsistent with that of numpy.

To Reproduce Here is my Python code:

import numpy as np
np.random.seed(0)
print(np.random.randn(1, 4))

The output of this code is:

[[1.76405235 0.40015721 0.97873798 2.2408932 ]]

And here is my C++ code:

#include <iostream>
#include "NumCpp.hpp"

using namespace std;
int main(void) {
    nc::random::seed(0);
    cout << nc::random::randN<float>(nc::Shape(1, 4)) << endl;
    return 0;
}

The output is:

[[0.101919, -0.481323, -0.680603, 0.0649879, ]]

I want to know how to keep the behavior of random::seed in NumCpp consistent with numpy?

dpilger26 commented 1 year ago

The purpose of seeding the random generator is to guarantee that you can recreate the same randomness at a later time. However, NumCpp and NumPy use different random generators so using the same seed between them will NOT result in the same random numbers produced.