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
Problem for random seed #71
Comments
Sorry but I'm not able to reproduce your issue. Can you confirm that the below code doesn't work for you (same value every time)? #include "NumCpp.hpp"
#include <cstdlib>
#include <ctime>
#include <iostream>
int main()
{
auto seed = static_cast<nc::uint32>(std::time(NULL));
nc::random::seed(seed);
auto randValue = nc::random::randFloat<float>(0, 1);
std::cout << "seed = " << seed << " value = " << randValue << '\n';
return EXIT_SUCCESS;
} I've tried with both GCC-10 and Clang-9, and Boost 1.73. What compiler and Boost version are you using? |
Closing due to inactivity. I will open again if you still believe there is an issue. |
I'm also having trouble with seeds. It seems that your code:
works, while
is deterministic. Could be my use of double rather than float? |
Can you tell me the compiler and boost versions you are using? I'm once again not able to reproduce an issue. I've tried gcc and clang on Ubuntu, and VS2019 on Windows all appear to be working as expected (non-deterministic when seeding with Also, random::uniform is just a convenience wrapper around random::randFloat so it doesn't make any sense that one would work and the other wouldn't... template<typename dtype>
dtype uniform(dtype inLow, dtype inHigh)
{
STATIC_ASSERT_FLOAT(dtype);
return randFloat(inLow, inHigh);
} |
Interesting, I'm using MSVC 2019. It may just be my that my program has a bug then - it must be something to do with the way I'm using the seed. Thanks for verifying. |
I wrote my own basic random number generator for now - and it works as expected. Then I got curious to see whether I could reproduce my problem in a MWE, which apparently I can. This example closely replicates what I'm doing in my project and the strategy works with my own RNG.
|
It looks like you forgot to set the NumCpp random seed in your example code above. I made some slight modifications to your class ClassThatUsesRandomNumberGenerator {
RNGOptions options_;
public:
ClassThatUsesRandomNumberGenerator()
{
setSeed();
}
explicit ClassThatUsesRandomNumberGenerator(RNGOptions options)
: options_(options)
{
setSeed();
}
const RNGOptions &getOptions() const {
return options_;
}
void setOptions(const RNGOptions &options) {
options_ = options;
setSeed();
}
double randomUniformDouble(double low, double high){
return nc::random::uniform<double>(low, high);
}
int randomUniformInt(int low, int high){
return nc::random::randInt(low, high);
}
private:
void setSeed()
{
nc::random::seed(options_.getSeed());
}
}; |
Yep, that does indeed work as expected. Thanks. |
Here is my code, but the final executable program products the same number every time it starts. However, the following code can generate different numbers each time.
The text was updated successfully, but these errors were encountered: