added random_device to intarray.cpp#5
Conversation
| random_array[i] = rand() % 2; | ||
| try | ||
| { | ||
| std::random_device rd; |
There was a problem hiding this comment.
Move std::random device outside of the loop, calling it multiple times is redundant.
There was a problem hiding this comment.
See here for an example of it being used. Otp we discussed and I read up some more on it.
std::random_device can fail
- On construction, ie
std::random_device [name], throwing an exception and exiting the whole program if left uncaught. - Or when requesting a random number, ie
rd() % 2. Currently your code would catch this and usesrand()in the same spot.
I think you have 2 options here. One is the easy way out and the other is more proper/more involved, but whichever you want to do is fine with me:
- For our purposes it's fine if the whole program exits with an exception if
std::random_devicefails. - Possibly, a better way to construct this is to have 2 separate classes, one
IntArraySrandandIntArrayRandDeviceor something like that, and in a parent classIntArrayattempt to build the array usingIntArrayRandDeviceand if that fails, build it usingIntArraySrand. This way you could havestd::random_devicecontained within theIntArrayRandDeviceclass and so if it fails when constructing the class you know you can't use it anymore.
There was a problem hiding this comment.
The second option using the classes would also allow for easier reuse later; notably, being able to combine the generators in different ways for different purposes that appeal better to the different rngs' strengths.
| { | ||
| std::random_device rd; | ||
| random_array[i] = rd() % 2; | ||
| } catch(...) |
There was a problem hiding this comment.
Try catch rather than actually checking for errors? EDIT: I actually like the try catch. See above.
| random_array[i] = rand() % 2; | ||
| try | ||
| { | ||
| std::random_device rd; |
There was a problem hiding this comment.
See here for an example of it being used. Otp we discussed and I read up some more on it.
std::random_device can fail
- On construction, ie
std::random_device [name], throwing an exception and exiting the whole program if left uncaught. - Or when requesting a random number, ie
rd() % 2. Currently your code would catch this and usesrand()in the same spot.
I think you have 2 options here. One is the easy way out and the other is more proper/more involved, but whichever you want to do is fine with me:
- For our purposes it's fine if the whole program exits with an exception if
std::random_devicefails. - Possibly, a better way to construct this is to have 2 separate classes, one
IntArraySrandandIntArrayRandDeviceor something like that, and in a parent classIntArrayattempt to build the array usingIntArrayRandDeviceand if that fails, build it usingIntArraySrand. This way you could havestd::random_devicecontained within theIntArrayRandDeviceclass and so if it fails when constructing the class you know you can't use it anymore.
No description provided.