mercredi 20 mars 2019

Making full use of 256 random bits when you just need 1 bit

I am doing some Monte Carle simulations. A CSPRNG is too expensive, so I'm using the xoshiro256** generator instead, which as its name suggests fills four 64-bit unsigned integers with random bits.

In my use case, only one random bit is required each time, but only extracting the lowest bit seems a huge waste.

static uint64_t s[4] = { /* SEED */ };

static inline unsigned random_bernoulli(void) {
    next(s);
    return s[0] & 1U;
}

How can I make full use of the 256 bits, preferably in a not-so-CPU-intensive way? Or, is the lowest bit random enough so my current approach is good?




Aucun commentaire:

Enregistrer un commentaire