00001 /* LIBGUL - Geometry Utility Library 00002 * Copyright (C) 1998-1999 Norbert Irmer 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this library; if not, write to the 00016 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 * Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #ifndef GUMA_RANDOM_H 00021 #define GUMA_RANDOM_H 00022 00023 namespace guma { 00024 00025 /*---------------------------------------------------------------------------- 00026 System dependent function to get a seed for pseudo random number generators 00027 ----------------------------------------------------------------------------*/ 00028 GULAPI gul::uint32 GetSeed(); 00029 00030 /*------------------------------------------------------------------ 00031 Function for generating pseudo random numbers with DES crypto 00032 ------------------------------------------------------------------*/ 00033 GULAPI void PseudoDes( gul::uint32 *lword, gul::uint32 *irword ); 00034 00035 /*------------------------------------------------------------------ 00036 Class which can be used as a parameter for templates which need a 00037 random number generator 00038 ------------------------------------------------------------------*/ 00039 class random_des 00040 { 00041 public: 00042 GULAPI static gul::uint32 cycle; 00043 GULAPI static gul::uint32 sequence; 00044 GULAPI static bool initialized; 00045 00046 // can be used for debugging to with a certain sequence/cycle 00047 static double des_random( bool reset, gul::uint32 *idum ) 00048 { 00049 const double factor = 1.0/((double)LL(0x100000000)); 00050 gul::uint32 irword,lword; 00051 00052 if( reset ) 00053 { 00054 sequence = *idum; 00055 *idum = 1; 00056 } 00057 irword = *idum; 00058 lword = sequence; 00059 00060 PseudoDes( &lword, &irword ); 00061 00062 (*idum)++; 00063 00064 return ((double)irword) * factor; 00065 } 00066 00067 static double Random( void ) 00068 { 00069 if( !initialized ) 00070 { 00071 initialized = true; 00072 cycle = GetSeed(); 00073 return des_random( true, &cycle ); 00074 } 00075 return des_random( false, &cycle ); 00076 } 00077 }; 00078 00079 } 00080 00081 #endif