home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / Rw / random.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  3.2 KB  |  96 lines

  1. #ifndef __RANDOM_H
  2. #define __RANDOM_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. /***************************************************************************
  5.  *
  6.  * random.h - Header for the Standard Library random generator
  7.  *
  8.  * $Id: random.h,v 1.6 1996/08/28 01:30:28 smithey Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * Copyright (c) 1994
  13.  * Hewlett-Packard Company
  14.  *
  15.  * Permission to use, copy, modify, distribute and sell this software
  16.  * and its documentation for any purpose is hereby granted without fee,
  17.  * provided that the above copyright notice appear in all copies and
  18.  * that both that copyright notice and this permission notice appear
  19.  * in supporting documentation.  Hewlett-Packard Company makes no
  20.  * representations about the suitability of this software for any
  21.  * purpose.  It is provided "as is" without express or implied warranty.
  22.  *
  23.  *
  24.  ***************************************************************************
  25.  *
  26.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  27.  *
  28.  * This computer software is owned by Rogue Wave Software, Inc. and is
  29.  * protected by U.S. copyright laws and other laws and by international
  30.  * treaties.  This computer software is furnished by Rogue Wave Software,
  31.  * Inc. pursuant to a written license agreement and may be used, copied,
  32.  * transmitted, and stored only in accordance with the terms of such
  33.  * license and with the inclusion of the above copyright notice.  This
  34.  * computer software or any other copies thereof may not be provided or
  35.  * otherwise made available to any other person.
  36.  *
  37.  * U.S. Government Restricted Rights.  This computer software is provided
  38.  * with Restricted Rights.  Use, duplication, or disclosure by the
  39.  * Government is subject to restrictions as set forth in subparagraph (c)
  40.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  41.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  42.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  43.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  44.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  45.  *
  46.  **************************************************************************/
  47.  
  48. #include <stdcomp.h>
  49. #include <rw/stddefs.h>    
  50. #ifdef _RWSTD_MULTI_THREAD
  51. #include <rw/stdmutex.h> 
  52. #endif
  53.  
  54. #ifndef _RWSTD_NO_NAMESPACE
  55. namespace __rwstd {
  56. #endif
  57.  
  58. class _RWSTDExport  __random_generator
  59. {
  60.   protected:
  61.  
  62.     enum { LENGTH = 55 };
  63.  
  64.     unsigned long table[LENGTH];
  65.     size_t        index1;
  66.     size_t        index2;
  67. #ifdef _RWSTD_MULTI_THREAD
  68.     _RWSTDMutex    mutex;
  69. #endif
  70.  
  71.     void seed (unsigned long j);
  72.  
  73.   public:
  74.  
  75.     unsigned long operator() (unsigned long limit)
  76.     {
  77. #ifdef _RWSTD_MULTI_THREAD
  78.         _RWSTDGuard guard(mutex);
  79. #endif
  80.         index1 = (index1 + 1) % LENGTH;
  81.         index2 = (index2 + 1) % LENGTH;
  82.         table[index1] = table[index1] - table[index2];
  83.         return table[index1] % limit;
  84.     }
  85.  
  86.     __random_generator (unsigned long j) { this->seed(j); }
  87.    
  88. };
  89.  
  90. #ifndef _RWSTD_NO_NAMESPACE
  91. }
  92. #endif
  93.  
  94. #pragma option pop
  95. #endif /* __RANDOM_H */
  96.