1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

lib: Share lvm_even_rand for random numbers.

This commit is contained in:
Alasdair G Kergon 2014-04-04 01:26:19 +01:00
parent e7b8e0a10c
commit 12ddaa5f10
4 changed files with 28 additions and 24 deletions

View File

@ -1,5 +1,6 @@
Version 2.02.106 -
====================================
Export lvm_even_rand() for controlled provision of random numbers.
Add lvmthin man page to section 7.
Extend internal validation of lv names size is less then 128 chars.
Fail in resume for lvrename will result in failing command.

View File

@ -1057,29 +1057,6 @@ uint64_t extents_from_size(struct cmd_context *cmd, uint64_t size,
return size / extent_size;
}
/*
* Return random integer in [0,max) interval
*
* The loop 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.
*/
static unsigned _even_rand( unsigned *seed, unsigned max )
{
unsigned r, ret;
/* make sure distribution is even */
do {
r = (unsigned) rand_r( seed );
ret = r % max;
} while ( r - ret > RAND_MAX - max );
return ret;
}
static dm_bitset_t _bitset_with_random_bits(struct dm_pool *mem, uint32_t num_bits,
uint32_t num_set_bits, unsigned *seed)
{
@ -1102,7 +1079,7 @@ static dm_bitset_t _bitset_with_random_bits(struct dm_pool *mem, uint32_t num_bi
/* Perform loop num_set_bits times, selecting one bit each time */
while (i++ < num_bits) {
/* Select a random bit between 0 and (i-1) inclusive. */
bit_selected = _even_rand(seed, i);
bit_selected = lvm_even_rand(seed, i);
/*
* If the bit was already set, set the new bit that became

View File

@ -118,3 +118,24 @@ int read_urandom(void *buf, size_t len)
return 1;
}
/*
* Return random integer in [0,max) interval
*
* The loop 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.
*/
unsigned lvm_even_rand(unsigned *seed, unsigned max)
{
unsigned r, ret;
do {
r = (unsigned) rand_r(seed);
ret = r % max;
} while (r - ret > RAND_MAX - max);
return ret;
}

View File

@ -32,6 +32,11 @@ int lvm_getpagesize(void);
*/
int read_urandom(void *buf, size_t len);
/*
* Return random integer in [0,max) interval
*/
unsigned lvm_even_rand(unsigned *seed, unsigned max);
# ifndef HAVE_SIGINTERRUPT
# define siginterrupt(sig, flag) \
do { \