Re: [PATCH 2/4] xkcd: enable more efficient random number generator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2016-04-01 at 12:30 +0100, Daniel P. Berrange wrote:
> XKCD comic 221 provides a very efficient algorithm for
> a random number generator, which we can utilize in
> libvirt. This is enabled simply
> 
>    LIBVIRT_XKCD=221 /usr/sbin/libvirt
> 
> The effect can be seen when defining new guests without
> a MAC address
> 
>   # virsh define demo.xml
>   # virsh dumpxml demo | grep mac
>       <mac address='52:54:00:04:04:04'/>
> 
> Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
> ---
>  src/util/virrandom.c | 26 ++++++++++++++++----------
>  1 file changed, 16 insertions(+), 10 deletions(-)
> 
> diff --git a/src/util/virrandom.c b/src/util/virrandom.c
> index 67a8bd0..31d2a8f 100644
> --- a/src/util/virrandom.c
> +++ b/src/util/virrandom.c
> @@ -34,6 +34,7 @@
>  #include "virerror.h"
>  #include "virlog.h"
>  #include "virstring.h"
> +#include "virxkcd.h"
>  
>  #define VIR_FROM_THIS VIR_FROM_NONE
>  
> @@ -107,19 +108,24 @@ uint64_t virRandomBits(int nbits)
>          return 0;
>      }
>  
> -    virMutexLock(&randomLock);
> +    if (virXKCDIsEnabled(221)) {
> +        return 4; /* chosen by fair dice roll.
> +                   * guaranteed to be random. */
> +    } else {
> +        virMutexLock(&randomLock);
>  
> -    while (nbits > RANDOM_BITS_PER_ITER) {
> -        random_r(&randomData, &bits);
> -        ret = (ret << RANDOM_BITS_PER_ITER) | (bits & RANDOM_BITS_MASK);
> -        nbits -= RANDOM_BITS_PER_ITER;
> -    }
> +        while (nbits > RANDOM_BITS_PER_ITER) {
> +            random_r(&randomData, &bits);
> +            ret = (ret << RANDOM_BITS_PER_ITER) | (bits & RANDOM_BITS_MASK);
> +            nbits -= RANDOM_BITS_PER_ITER;
> +        }
>  
> -    random_r(&randomData, &bits);
> -    ret = (ret << nbits) | (bits & ((1 << nbits) - 1));
> +        random_r(&randomData, &bits);
> +        ret = (ret << nbits) | (bits & ((1 << nbits) - 1));
>  
> -    virMutexUnlock(&randomLock);
> -    return ret;
> +        virMutexUnlock(&randomLock);
> +        return ret;
> +    }
>  }

Sorry, but we don't have any proof that the value '4' was
actually obtained with a fair dice roll.

If you can provide video footage of either yourself or
Randall Munroe rolling a dice and getting a four, then
it's an ACK from me.

Cheers.

-- 
Andrea Bolognani
Software Engineer - Virtualization Team

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]