
YARANDOM                                                        26-DEC-1995 19:55:55    VAX C      V3.2-044                 Page 1
V1.0                                                            26-DEC-1995 13:14:03    YARANDOM.C;2 (1)



    1        	/* ya_random -- Yet Another Random Number Generator.
    2        	
    3        	   The unportable mess that is rand(), random(), drand48() and friends led me
    4        	   to ask Phil Karlton <karlton@netscape.com> what the Right Thing to Do was.
    5        	   He responded with this.  It is non-cryptographically secure, reasonably
    6        	   random (more so than anything that is in any C library), and very fast.
    7        	
    8        	   I don't understand how it works at all, but he says "look at Knuth,
    9        	   Vol. 2 (original edition), page 26, Algorithm A.  In this case n=55,
   10        	   k=20 and m=2^32."
   11        	
   12        	   So there you have it.
   13        	 */
   14        	
   15        	#ifndef VMS
   16   X    	#include <unistd.h>   /* for getpid() */
   17   X    	#include <sys/time.h> /* for gettimeofday() */
   18        	#else
   19        	#include unixlib       /* for getpid() */
  103        	#include "unix_time.h" /* for gettimeofday() */
  299        	#endif
  300        	
  301        	
  302        	/* The following 'random' numbers are taken from CRC, 18th Edition, page 622.
  303        	   Each array element was taken from the corresponding line in the table,
  304        	   except that a[0] was from line 100. 8s and 9s in the table were simply
  305        	   skipped. The high order digit was taken mod 4.
  306        	 */
  307        	#define VectorSize 55
  308        	static unsigned int a[VectorSize] = {
  309        	 035340171546, 010401501101, 022364657325, 024130436022, 002167303062, /*  5 */
  310        	 037570375137, 037210607110, 016272055420, 023011770546, 017143426366, /* 10 */
  311        	 014753657433, 021657231332, 023553406142, 004236526362, 010365611275, /* 14 */
  312        	 007117336710, 011051276551, 002362132524, 001011540233, 012162531646, /* 20 */
  313        	 007056762337, 006631245521, 014164542224, 032633236305, 023342700176, /* 25 */
  314        	 002433062234, 015257225043, 026762051606, 000742573230, 005366042132, /* 30 */
  315        	 012126416411, 000520471171, 000725646277, 020116577576, 025765742604, /* 35 */
  316        	 007633473735, 015674255275, 017555634041, 006503154145, 021576344247, /* 40 */
  317        	 014577627653, 002707523333, 034146376720, 030060227734, 013765414060, /* 45 */
  318        	 036072251540, 007255221037, 024364674123, 006200353166, 010126373326, /* 50 */
  319        	 015664104320, 016401041535, 016215305520, 033115351014, 017411670323  /* 55 */
  320        	};
  321        	
  322        	static int i1, i2;
  323        	
  324        	unsigned int ya_random()
  325        	{
  326    1   	  register int ret = a[i1] + a[i2];
  327    1   	  a[i1] = ret;
  328    1   	  i1 = i1 >= VectorSize ? 0 : i1 + 1;
  329    1   	  i2 = i2 >= VectorSize ? 0 : i2 + 1;
  330    1   	  return ret;
  331    1   	}
  332        	
  333        	void ya_rand_init(seed)

YARANDOM                                                        26-DEC-1995 19:55:55    VAX C      V3.2-044                 Page 2
V1.0                                                            26-DEC-1995 13:14:03    YARANDOM.C;2 (1)

  334        	   register unsigned int seed;
  335        	{
  336    1   	  int i;
  337    1   	  if (seed == 0)
  338    1   	    {
  339    2   	      struct timeval tp;
  340    2   	      struct timezone tzp;
  341    2   	      gettimeofday(&tp, &tzp);
  342    2   	      /* ignore overflow */
  343    2   	      seed = (999*tp.tv_sec) + (1001*tp.tv_usec) + (1003 * getpid());
  344    2   	    }
  345    1   	
  346    1   	  a[0] += seed;
  347    1   	  for (i = 1; i < VectorSize; i++)
  348    1   	    {
  349    2   	      seed = a[i-1]*1001 + seed*999;
  350    2   	      a[i] += seed;
  351    2   	    }
  352    1   	
  353    1   	  i1 = a[0] % VectorSize;
  354    1   	  i2 = (i1 + 024) % VectorSize;
  355    1   	}

 
 
Command Line
------------
 
CC/DEFINE=VMS/LIS YARANDOM.C

