In the gcc implementation this works trivially; the parameters are only applied as simple multiply and shift of the actual algorithm’s output. But I could imagine other algorithms to have problems with a special case like this. Should I better build in an external safe-guard, or is it fine to just give std::normal_distribution‘s constructor a 0 as the standard deviation parameter to get a “non-random distribution”, i.e. one that always yields the mean value?
(Performance aside)
The standard says the following (§26.5.8.4.4):
As such a standard derivation of
0is explicitly forbidden by the standard and therefore not guaranteed to work. Therefore building an external safeguard seems like a good ideaEven if the typical implemention would work on a standard derivation of 0 (not sure if that is the case), I could imagine an implementation which tests for such a case and throws an exception when the standard derivation is zero and throws an exception if it is not (to ensure that the code is portable). As an alternative it is possible that the code will divide by the standard derivation somewhere, which would also be problematic with a derivation of
0.