As the documentation says:
The effect is undefined if this is not one of
short
,int
,long
,long long
,unsigned short
,unsigned int
,unsigned long
, orunsigned long long
.
If I don't care about the range, I can just mask off the bits of a larger type to generate random numbers. If not, it's more complex. Why aren't the byte types provided just by default?
There is a library working group unresolved issue on this uniform_int_distribution<unsigned char> should be permitted and it says, amongst other things:
The proposed resolution is to change the restriction to allow standard integer types:
and:
This gets you unsigned / signed char although not uint8_t or int8_t but they are likely equivalent. Extended integral types were exluded to simplify the wording and maximize consensus:
Note, this excludes
char
since it is implementation defined whetherchar
is signed or not.Note this topic was also brought up in the std-discussion list.
Jonathan Wakely notes this proposal is controversial and commented that his notes from the last discussion include the following:
He suggests adding a member to
random_device
to provide single bytes, which is seems like a reasonable alternative.