Avoid rand()
in the legacy tr1
test suite by StephanTLavavej · Pull Request #4921 · microsoft/STL (original) (raw)
Our bosses and boss-like entities heard that rand() is considered harmful and are starting a push to avoid unnecessary use.
Almost all of our mentions of rand
are Standard-mandated (or Standard stable name citations), but I found a few unnecessary uses in our legacy tr1
test suite. We avoid cleaning up its code unless there's a specific reason to mess with it, and this easily qualifies.
tr1/tests/algorithm
- Give
rand_gen
anmt19937
data member. (Default construction is fine; we weren't attempting to seedrand()
before.) This is purely masking bits with& 0xfffff
, so there's no need for auniform_int_distribution
. - Replace the
frand()
wrapper with anrng_func
lambda. To avoid/clr:pure
problems with init-captures, I'm capturingmt
by reference (so the lambda doesn't need to bemutable
and doesn't need to be passed viaref()
). We need to adapt the half-open interface touniform_int_distribution
's fully-closed range.
- Give
tr1/tests/memory2
- Construct a local
mt19937 mt;
within thestatic void ctors()
worker function. (It's important to not use a global engine with multiple threads!) Again, we weren't attempting to seedrand()
, so default-constructingmt19937
is fine. And because we're just looking at a bit, we don't need auniform_int_distribution
.
- Construct a local