home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cm3.zip / LRANDOM.C < prev   
Text File  |  1995-02-09  |  2KB  |  61 lines

  1. /* ================ Random Number Generator from BSD ======================= */
  2.  
  3. /*
  4.  * Copyright (c) 1983 Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that: (1) source distributions retain this entire copyright
  9.  * notice and comment, and (2) distributions including binaries display
  10.  * the following acknowledgement:  ``This product includes software
  11.  * developed by the University of California, Berkeley and its contributors''
  12.  * in the documentation or other materials provided with the distribution
  13.  * and in all advertising materials mentioning features or use of this
  14.  * software. Neither the name of the University nor the names of its
  15.  * contributors may be used to endorse or promote products derived
  16.  * from this software without specific prior written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. static long randtbl[32]    = { 0L,
  23.     0x9a319039L, 0x32d9c024L, 0x9b663182L, 0x5da1f342L, 
  24.     0xde3b81e0L, 0xdf0a6fb5L, 0xf103bc02L, 0x48f340fbL, 
  25.     0x7449e56bL, 0xbeb1dbb0L, 0xab5c5918L, 0x946554fdL, 
  26.     0x8c2e680fL, 0xeb3d799fL, 0xb11ee0b7L, 0x2d436b86L, 
  27.     0xda672e2aL, 0x1588ca88L, 0xe369735dL, 0x904f35f7L, 
  28.     0xd7158fd6L, 0x6fa6f051L, 0x616e6b96L, 0xac94efdcL, 
  29.     0x36413f93L, 0xc622c298L, 0xf5a42ab8L, 0x8a88d77bL, 
  30.     0xf5ad9d0eL, 0x8999220bL, 0x27fb47b9L
  31. };
  32.  
  33. static  long *fptr    = &randtbl[4];
  34. static  long *rptr    = &randtbl[1];
  35. static  long *state    = &randtbl[1];
  36. static  long *end_ptr = &randtbl[32];
  37. /*
  38.  * Returns a really good 31-bit random number.
  39.  */
  40. static long
  41. lrandom()
  42. {
  43. long i;
  44.     
  45.     *fptr += *rptr;
  46.     i = (*fptr >> 1) & 0x7fffffffUL;    /* chucking least random bit */
  47.     if(++fptr >= end_ptr)
  48.     {
  49.         fptr = state;
  50.         ++rptr;
  51.     }
  52.     else
  53.     {
  54.         if(++rptr >= end_ptr)  
  55.             rptr = state;
  56.     }
  57.     return( i );
  58. }
  59. /* ========================= END Random Number Generator ==================== */
  60.  
  61.