From: Goffredo Baroncelli <kreijack@inwind.it>
To: linux-btrfs <linux-btrfs@vger.kernel.org>
Subject: Re: [PATCH 1/2] btrfs-progs: utils: Introduce new pseudo random API
Date: Wed, 25 May 2016 21:06:07 +0200 [thread overview]
Message-ID: <5745F79F.1000203@inwind.it> (raw)
In-Reply-To: <1464149645-4308-1-git-send-email-quwenruo@cn.fujitsu.com>
On 2016-05-25 06:14, Qu Wenruo wrote:
> +void rand_seed(u64 seed)
> +{
> + int i;
> + /* only use the last 48 bits */
> + for (i = 0; i < 3; i++) {
> + seeds[i] = (unsigned short)(seed ^ (unsigned short)(-1));
> + seed >>= 16;
> + }
> + seed_initlized = 1;
> +}
> +
> +u32 rand_u32(void)
> +{
> + struct timeval tv;
> +
> + if (seed_initlized)
> + return nrand48(seeds);
> +
> + /*
> + * It's possible to use /dev/random, but we don't need that true
> + * random number nor want to wait for entropy,
> + * since we're only using random API to do corruption to test.
> + * Time and pid/ppid based seed would be good enough, and won't
> + * cause sleep for entropy pool.
> + */
> + gettimeofday(&tv, 0);
> + seeds[0] = getpid() ^ (tv.tv_sec & 0xFFFF);
> + seeds[1] = getppid() ^ (tv.tv_usec & 0xFFFF);
> + seeds[2] = (tv.tv_sec ^ tv.tv_usec) >> 16;
> + seed_initlized = 1;
> +
> + return (u32)nrand48(seeds);
> +}
Just for my curiosity, which is the advantage of rand48() respect to rand() if we utilize only the lower 32 bit ? It wouldn't be more simple to use:
#define rand_seed(x) srand((int)(x))
/*
* NOTE: not for cryptographic use
*/
u32 rand_u32(void)
{
static __thread int initialized = 0;
if (!initialized) {
struct timeval tv;
gettimeofday(&tv, 0);
rand_seed(tv.tv_usec | (tv.tv_sec ^ getpid() ^ getppid()) << 16);
initialized = 1;
}
return (u32)rand();
}
this in order to avoid the mess of the 48 bit ?
BR
G.Baroncelli
--
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it>
Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5
prev parent reply other threads:[~2016-05-25 19:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-25 4:14 [PATCH 1/2] btrfs-progs: utils: Introduce new pseudo random API Qu Wenruo
2016-05-25 4:14 ` [PATCH 2/2] btrfs-progs: Use new random number API Qu Wenruo
2016-05-25 16:15 ` [PATCH 1/2] btrfs-progs: utils: Introduce new pseudo random API Noah Massey
2016-05-26 0:29 ` Qu Wenruo
2016-05-25 19:06 ` Goffredo Baroncelli [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=5745F79F.1000203@inwind.it \
--to=kreijack@inwind.it \
--cc=linux-btrfs@vger.kernel.org \
/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.