From: Przemyslaw Iskra <sparky@pld-linux.org>
To: lvm-devel@redhat.com
Subject: [PATCH] Don't use floor() in _bitset_with_random_bits
Date: Sat, 7 Aug 2010 03:51:11 +0200 [thread overview]
Message-ID: <20100807015111.GA25844@pld-linux.org> (raw)
In-Reply-To: <87hbj7k0iy.fsf@twilight.int.mornfall.net.>
On Fri, Aug 06, 2010 at 09:24:05PM +0200, Petr Rockai wrote:
> Hi,
>
> Przemyslaw Iskra <sparky@pld-linux.org> writes:
> > Use _even_rand() function instead of floor() in
> > _bitset_with_random_bits(). floor() function is missing in dietlibc (on
> > architectures other than x86). Moreover using floor() to clip rand
> > results does not assure even result distribution.
> > _even_rand() uses integer arithmetic only and is designed to return evenly
> > distributed results.
> >
> > Signed-off-by: Przemyslaw Iskra <sparky@pld-linux.org>
>
> Reviewed-by: Petr Rockai <prockai@redhat.com>
>
> Looks OK to me. It took a while to decipher what is the exact meaning of
> the loop in _even_rand (to a non-pseudorandomness-expert) but I am
> fairly comfortable with it now. If I understand this correctly, it
> rejects numbers that come from an "incomplete" slice of the RAND_MAX
> space (considering the number space [0, RAND_MAX] is divided into some
> "max"-sized slices and at most a single smaller slice, between [n*max,
> RAND_MAX] for suitable n -- numbers from this last slice are discarded
> because they could distort the distribution in favour of smaller
> numbers).
That's exactly what it does.
However, right now it cutts off the last slice even if it's complete.
You could replace:
> > + } while ( r - ret >= RAND_MAX - max );
With:
+ } while ( r - ret > (unsigned) RAND_MAX + 1 - max );
Result distribution is correct in both cases. It just avoids the branch
in 0.00something %.
Przemyslaw Iskra
--
____ Sparky{PI] -- Przemyslaw _ ___ _ _ ......... LANG...Pl,Ca,Es,En
/____) ___ ___ _ _ || Iskra | | _ \| | | : WWW...ppcrcd.pld-linux.org
\____\| -_)'___| ||^'||//\\// < | _/| | | : WWW2..............rsget.pl
(____/|| (_-_|_|| ||\\ || |_ |_| |_| _| : Mail..sparky@pld-linux.org
prev parent reply other threads:[~2010-08-07 1:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-30 17:05 [PATCH] Don't use floor() in _bitset_with_random_bits Przemyslaw Iskra
2010-08-06 19:24 ` Petr Rockai
2010-08-07 1:51 ` Przemyslaw Iskra [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100807015111.GA25844@pld-linux.org \
--to=sparky@pld-linux.org \
--cc=lvm-devel@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.