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

17.29 and 17.30 #482

Open
MRamez opened this issue Jun 7, 2016 · 0 comments
Open

17.29 and 17.30 #482

MRamez opened this issue Jun 7, 2016 · 0 comments

Comments

@MRamez
Copy link

MRamez commented Jun 7, 2016

Hi,
In random_gen functions, e and ud are static. Because they are static, they will be initialized only once. This means their range and seed cannot be changed after the first call to random_gen. For example:
random_gen(0, 1, 9);
random_gen(3, 0, 100);
The second call to random_gen returns a random unsigned from [1, 9] and the seed that e uses is 0.
This is my proposed solution for 17.30 (the same can be applied to 17.29):

template <unsigned seed, unsigned max, unsigned min>
unsigned random_gen()
{
   static std::default_random_engine e(seed);
   static std::uniform_int_distribution<unsigned> ud(min, max);
   return ud(e);
}

Thanks

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

1 participant