All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: George Spelvin <lkml@sdf.org>
Cc: LKML <linux-kernel@vger.kernel.org>, "Theodore Ts'o" <tytso@mit.edu>
Subject: Re: [RFC PATCH] random: add get_random_max() function
Date: Sun, 24 Mar 2019 21:47:50 +0100	[thread overview]
Message-ID: <CAHmME9p3TR6ESekKRQxFsfQe3rZp7T4-4WLOn-SL98zUnAo2DQ@mail.gmail.com> (raw)
In-Reply-To: <201903241244.x2OCiL8P011277@sdf.org>

I generally use a slightly simpler algorithm in various different projects:

//[0, bound)
static unsigned long random_bounded(unsigned long bound)
{
       unsigned long ret;
       const unsigned long max_mod_bound = (1 + ~bound) % bound;

       if (bound < 2)
               return 0;
       do
               ret = random_integer();
       while (ret < max_mod_bound);
       return ret % bound;
}

//[min, max_plus_one)
static unsigned long random_range(unsigned long min, unsigned long max_plus_one)
{
       return random_bounded(max_plus_one - min) + min;
}

Is the motivation behind using Lemire that you avoid the division (via
the modulo) in favor of a multiplication?

       reply	other threads:[~2019-03-24 20:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <201903241244.x2OCiL8P011277@sdf.org>
2019-03-24 20:47 ` Jason A. Donenfeld [this message]
2019-03-25  0:28   ` [RFC PATCH] random: add get_random_max() function George Spelvin
2019-03-25  1:14   ` George Spelvin
     [not found] ` <201903270655.x2R6tDjo020894@sdf.org>
2019-03-28 10:00   ` [RFC PATCH v2] " George Spelvin

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=CAHmME9p3TR6ESekKRQxFsfQe3rZp7T4-4WLOn-SL98zUnAo2DQ@mail.gmail.com \
    --to=jason@zx2c4.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkml@sdf.org \
    --cc=tytso@mit.edu \
    /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.