[libvirt] [PATCH v2] Add virRandom() API to generate numbers with non-power-of-2 limit

From: "Daniel P. Berrange" <berrange@redhat.com> The current virRandomBits() API is only usable if the caller wants a random number in the range [0, (n-1)] where n is a power of two. This adds a virRandom() API which generates a double in the range [0.0,1.0] with 48 bits of entropy. It then also adds a virRandomInt(uint32_t max) API which generates an unsigned in the range [0,@max] Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/libvirt_private.syms | 2 ++ src/util/virrandom.c | 32 ++++++++++++++++++++++++++++++++ src/util/virrandom.h | 2 ++ 3 files changed, 36 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 018d3a9..1905d6f 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1641,8 +1641,10 @@ virPidFileDeletePath; # virrandom.h +virRandom; virRandomBits; virRandomGenerateWWN; +virRandomInt; # virsocketaddr.h diff --git a/src/util/virrandom.c b/src/util/virrandom.c index 50bed46..2092afc 100644 --- a/src/util/virrandom.c +++ b/src/util/virrandom.c @@ -23,6 +23,7 @@ #include <stdlib.h> #include <inttypes.h> +#include <math.h> #include "virrandom.h" #include "threads.h" @@ -108,6 +109,37 @@ uint64_t virRandomBits(int nbits) return ret; } + +/** + * virRandom: + * + * Generate an evenly distributed random number between [0.0,1.0] + * + * Return: a random number with 48 bits of entropy + */ +double virRandom(void) +{ + uint64_t val = virRandomBits(48); + + return ldexp(val, -48); +} + + +/** + * virRandomInt32: + * @max: upper limit + * + * Generate an evenly distributed random integer between [0,@max] + * + * Return: a random number between [0,@max] + */ +uint32_t virRandomInt(uint32_t max) +{ + double val = virRandom(); + return val * max; +} + + #define QUMRANET_OUI "001a4a" #define VMWARE_OUI "000569" #define MICROSOFT_OUI "0050f2" diff --git a/src/util/virrandom.h b/src/util/virrandom.h index 29a055d..bd34c94 100644 --- a/src/util/virrandom.h +++ b/src/util/virrandom.h @@ -25,6 +25,8 @@ # include "internal.h" uint64_t virRandomBits(int nbits); +double virRandom(void); +uint32_t virRandomInt(uint32_t max); int virRandomGenerateWWN(char **wwn, const char *virt_type); #endif /* __VIR_RANDOM_H__ */ -- 1.7.11.2

On 08/14/2012 05:13 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The current virRandomBits() API is only usable if the caller wants a random number in the range [0, (n-1)] where n is a power of two.
This one is correct (it could also be written '[0, n)').
This adds a virRandom() API which generates a double in the range [0.0,1.0] with 48 bits of entropy. It then also adds a
s/]/)/ (the range includes 0.0, but excludes 1.0)
virRandomInt(uint32_t max) API which generates an unsigned in the range [0,@max]
s/]/)/ (the range includes 0, but excludes @max; it could also be written '[0,@max-1]')
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/libvirt_private.syms | 2 ++ src/util/virrandom.c | 32 ++++++++++++++++++++++++++++++++ src/util/virrandom.h | 2 ++ 3 files changed, 36 insertions(+)
+++ b/src/util/virrandom.c @@ -23,6 +23,7 @@
#include <stdlib.h> #include <inttypes.h> +#include <math.h>
Hmm, the gnulib module 'ldexp' is currently LGPLv3+, but then again, gnulib doesn't document any glaring issues fixed by the module, other than the module provides $(LDEXP_LIBM) as a handy way to decide in Makefile.am whether to link against -lm or not (some libc provide ldexp without -lm, other platforms require the extra library). Gnulib also provides the module 'ldexp-ieee', to fix platforms with broken handling of NaN, but we don't care about NaN in our usage. I'll ask on gnulib about whether the license of 'ldexp' can be relaxed; if Bruno agrees, then I'll do a followup patch to pull in that module. In the meantime, I don't have any objections to how you are using ldexp(), as long as you cross-compiled to mingw without issues.
+/** + * virRandom: + * + * Generate an evenly distributed random number between [0.0,1.0]
Same conversion of ] to ) as in the commit message.
+/** + * virRandomInt32:
s/32// - you must have changed the function name between documenting it and implementing it.
+ * @max: upper limit + * + * Generate an evenly distributed random integer between [0,@max] + * + * Return: a random number between [0,@max]
Same conversion of ] to ) as in the commit message. ACK with those spelling nits fixed. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 08/14/2012 05:13 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The current virRandomBits() API is only usable if the caller wants a random number in the range [0, (n-1)] where n is a power of two. This adds a virRandom() API which generates a double in the range [0.0,1.0] with 48 bits of entropy. It then also adds a virRandomInt(uint32_t max) API which generates an unsigned in the range [0,@max]
+uint32_t virRandomInt(uint32_t max) +{ + double val = virRandom(); + return val * max;
I just realized this always goes through the slower path of using a double; it might be worth doing a quick filter for a power of 2, and forward that directly through virRandomBits(); I'll propose a followup patch. I find it easier to write virRandomInt(256) than virRandomBits(8), for example. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
participants (2)
-
Daniel P. Berrange
-
Eric Blake