X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=utils%2Fyarandom.c;h=6d4a32385b4cb94fbbce4a068d240de9c9496f6b;hb=b81f521c5ad7022ac12db18ca8fcdd9fb063831e;hp=3d24943e0e9a3d960fa6264341999f6128712f3e;hpb=0ed85ca0e4b0eae40a4f50a51d63f2f41e45373a;p=xscreensaver diff --git a/utils/yarandom.c b/utils/yarandom.c index 3d24943e..6d4a3238 100644 --- a/utils/yarandom.c +++ b/utils/yarandom.c @@ -1,13 +1,23 @@ /* yarandom.c -- Yet Another Random Number Generator. + * Copyright (c) 1997-2010 by Jamie Zawinski + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation. No representations are made about the suitability of this + * software for any purpose. It is provided "as is" without express or + * implied warranty. + */ - The unportable mess that is rand(), random(), drand48() and friends led me +/* The unportable mess that is rand(), random(), drand48() and friends led me to ask Phil Karlton what the Right Thing to Do was. He responded with this. It is non-cryptographically secure, reasonably random (more so than anything that is in any C library), and very fast. I don't understand how it works at all, but he says "look at Knuth, Vol. 2 (original edition), page 26, Algorithm A. In this case n=55, - k=20 and m=2^32." + k=24 and m=2^32." So there you have it. @@ -99,17 +109,31 @@ ya_rand_init(unsigned int seed) #else gettimeofday(&tp); #endif - /* ignore overflow */ - seed = (999*tp.tv_sec) + (1001*tp.tv_usec) + (1003 * getpid()); + /* Since the multiplications will have a larger effect on the + upper bits than the lower bits, after every addition in the + seed, perform a bitwise rotate by an odd number, resulting + in a better distribution of randomness throughout the bits. + -- Brian Carlson, 2010. + */ +#define ROT(X,N) (((X)<<(N)) | ((X)>>((sizeof(unsigned int)*8)-(N)))) + seed = (999 * tp.tv_sec); + seed = ROT (seed, 11); + seed += (1001 * tp.tv_usec); + seed = ROT (seed, 7); + seed += (1003 * getpid()); + seed = ROT (seed, 13); } a[0] += seed; for (i = 1; i < VectorSize; i++) { - seed = a[i-1]*1001 + seed*999; + seed = seed*999; + seed = ROT (seed, 9); + seed += a[i-1]*1001; + seed = ROT (seed, 15); a[i] += seed; } i1 = a[0] % VectorSize; - i2 = (i1 + 024) % VectorSize; + i2 = (i1 + 24) % VectorSize; }