[Date Prev][Date Next] [Thread Prev][Thread Next] [Thread Index] [Date Index] [Author Index]

*From*: Eric Blake <eblake redhat com>*Cc*: libvir-list redhat com, Daniel J Walsh <dwalsh redhat com>*Subject*: Re: [libvirt] [PATCH 1/8] Add virRandom() API to generate numbers with non-power-of-2 limit*Date*: Fri, 10 Aug 2012 11:41:50 -0600

On 08/10/2012 10:06 AM, Eric Blake wrote: >> The alternative I wanted to use was drand48_t() which returns a double drand48_r >> in the range 0.0 -> 1.0, with 48 bits of entropy. You could multiply by >> 'max', and cast to int. But this isn't portable and is not fixed by >> GNULIB. > > Might not be too hard to make drand48_r into a gnulib module (harder > than clz, but not intractible, so I'll take a shot at it, but no > promises). Using drand48_r does still hit non-uniformity due to > rounding, but with much smaller fuzz factor (that is, instead of my > example of 50/25/25, you'd be more like 25.000004, 24.999993, > 24.999993), which I can live with as being in the noise. If it wasn't obvious, I was typing this off the cuff rather than giving actual numbers (still off the cuff, but real numbers would be more like 33.3333334, 33.3333333, 33.3333333 - something that actually adds up to 100% distribution). > > No need to do that. -lm already gives us a very nice function: ldexp(). > Basically, you take an integer in the range [0,1<<48), then multiply > that by 2**-48 using ldexp(). As we discussed on IRC, no need for drand48[_r], just use ldexp(virRandomBits(48), -48). -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

**Attachment:
signature.asc**

**References**:**[libvirt] [PATCH 0/8] Honour current process label when generating SELinux labels***From:*Daniel P. Berrange

**[libvirt] [PATCH 1/8] Add virRandom() API to generate numbers with non-power-of-2 limit***From:*Daniel P. Berrange

**Re: [libvirt] [PATCH 1/8] Add virRandom() API to generate numbers with non-power-of-2 limit***From:*Eric Blake

**Re: [libvirt] [PATCH 1/8] Add virRandom() API to generate numbers with non-power-of-2 limit***From:*Daniel P. Berrange

**Re: [libvirt] [PATCH 1/8] Add virRandom() API to generate numbers with non-power-of-2 limit***From:*Eric Blake