Date: Thu, 13 May 2021 10:14:59 -0400
On Thu, May 13, 2021 at 2:56 AM Lénárd Szolnoki via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Hi,
>
> Is uniform_int_distribution really trivial?
>
It's *relatively* trivial. ;)
When I'm writing code that needs both speed and cross-platform behavior, I
just do `g() % n`; for most applications the bias of "%" is lost in the
noise.
If you need absolute fairness, it'd be like
int mask = std::bit_ceil(n) - 1;
while (true) {
int r = g() & mask;
if (r < n) return r;
}
And if you want your code to work with URBG types `G` where `G::min() != 0`
and/or `G::max()+1 < n`, then it'd be like
std::independent_bits_engine<G, 64, uint64_t> realg(g);
and then work with `realg`.
I'm like 90% sure std::independent_bits_engine's behavior is
cross-platform, so you wouldn't have to reimplement it.
Actually, if your `G::max()` is too small, it's certainly a better idea to
reimplement your `G`, instead of trying to adapt a bad `G` via standard
tools.
(I realized at this point that I didn't know where to point people for a
good `G`, so I went and created
https://github.com/Quuxplusone/Xoshiro256ss/blob/main/xoshiro256ss.h so now
I can point people there. ;))
–Arthur
std-proposals_at_[hidden]> wrote:
> Hi,
>
> Is uniform_int_distribution really trivial?
>
It's *relatively* trivial. ;)
When I'm writing code that needs both speed and cross-platform behavior, I
just do `g() % n`; for most applications the bias of "%" is lost in the
noise.
If you need absolute fairness, it'd be like
int mask = std::bit_ceil(n) - 1;
while (true) {
int r = g() & mask;
if (r < n) return r;
}
And if you want your code to work with URBG types `G` where `G::min() != 0`
and/or `G::max()+1 < n`, then it'd be like
std::independent_bits_engine<G, 64, uint64_t> realg(g);
and then work with `realg`.
I'm like 90% sure std::independent_bits_engine's behavior is
cross-platform, so you wouldn't have to reimplement it.
Actually, if your `G::max()` is too small, it's certainly a better idea to
reimplement your `G`, instead of trying to adapt a bad `G` via standard
tools.
(I realized at this point that I didn't know where to point people for a
good `G`, so I went and created
https://github.com/Quuxplusone/Xoshiro256ss/blob/main/xoshiro256ss.h so now
I can point people there. ;))
–Arthur
Received on 2021-05-13 09:15:17