[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHmME9p3TR6ESekKRQxFsfQe3rZp7T4-4WLOn-SL98zUnAo2DQ@mail.gmail.com>
Date: Sun, 24 Mar 2019 21:47:50 +0100
From: "Jason A. Donenfeld" <Jason@...c4.com>
To: George Spelvin <lkml@....org>
Cc: LKML <linux-kernel@...r.kernel.org>,
"Theodore Ts'o" <tytso@....edu>
Subject: Re: [RFC PATCH] random: add get_random_max() function
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?
Powered by blists - more mailing lists