To answer your question: You can't.
The standard does not allow std::uniform_int_distribution
to be templated on char
, signed char
, or unsigned char
. Some believe that this is a defect in the standard, but it is the case.
You can simply template std::uniform_int_distribution
on unsigned short
, and set its min/max range to std::numeric_limits<unsigned char>::min()
and std::numeric_limits<unsigned char>::max()
, and then simply assign the result to an unsigned char
.
From the standard:
Throughout this subclause 26.5, the effect of instantiating a template:
[...]
e) that has a template type parameter named IntType
is undefined unless the corresponding template argument is cv-unqualified and is one of short
, int
, long
, long long
, unsigned short
, unsigned int
, unsigned long
, or unsigned long long
.
§26.5.1.1 [rand.req.genl]
Moreover:
You should use std::mt19937
to actually generate your random bytes. std::random_device
is liable to be slow, and likely produces entropy with statistical properties (i.e. suitability for use in cryptography) that you don't need.
That said, you will need to seed your std::mt19937
. You can do this with a std::random_device
and a std::seed_seq
.
Note that if you don't use a std::seed_seq
to seed your std::mt19937
, your std::mt19937
will be left with many, many zeroes in its internal state, and it will therefore take it quite a while to "warm up".
For more information on "warm up", see here.
distribution
(which adds processing overhead). – Ascariasisint
is four bytes? The standard certainly doesn't. – Girasolsizeof()
to know how many bytes it is... – Ascariasisint_least32_t
. – Semiconductorstd::uniform_int_distribution<uint8_t>
andstd::uniform_int_distribution<int8_t>
Allowed? – Malpighiaceous