Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

guma_random.cpp

Go to the documentation of this file.
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 #include "stdafx.h"
00021 
00022 #include "gul_types.h"
00023 #include "guma_random.h"
00024 
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 #include <fcntl.h>
00028 #ifdef _MSC_VER
00029   #include <io.h>  
00030 #else
00031   #include <unistd.h>
00032 #endif
00033 #include <time.h>
00034 
00035 #include <iostream>
00036 
00037 
00038 #if defined(__MINGW32__) || defined(_MSC_VER)
00039 
00040 /*-------------------------------------------------------------------------
00041   linear congruential method, used when platform has no random/srandom
00042   functions (ms-windows)
00043 -------------------------------------------------------------------------*/
00044 
00045 static long random_lastx;
00046 
00047 /*
00048  * Compute x = (7^5 * x) mod (2^31 - 1)
00049  * wihout overflowing 31 bits:
00050  *      (2^31 - 1) = 127773 * (7^5) + 2836
00051  * From "Random number generators: good ones are hard to find",
00052  * Park and Miller, Communications of the ACM, vol. 31, no. 10,
00053  * October 1988, p. 1195.
00054  */
00055 
00056 GULAPI long random()
00057 {
00058   long hi, lo, x;
00059 
00060   x = random_lastx;
00061   hi = x / 127773;
00062   lo = x % 127773;
00063   x = 16807 * lo - 2836 * hi;
00064   if (x <= 0)
00065     x += 0x7fffffff;
00066   random_lastx = x = (x & 0x7fffffff);
00067   return x;
00068 }
00069 
00070 GULAPI void srandom( long x )
00071 {
00072   random_lastx = x;
00073 }
00074 
00075 #endif
00076 
00077 
00078 namespace guma {
00079 
00080 using gul::uint32;
00081 using std::cout;
00082 
00083 GULAPI bool random_des::initialized = false;
00084 GULAPI uint32 random_des::sequence = 1;
00085 GULAPI uint32 random_des::cycle = 1;
00086 
00087 /*----------------------------------------------------------------------------
00088   System dependent function to get a seed for pseudo random number generators
00089 ----------------------------------------------------------------------------*/
00090 
00091 GULAPI uint32 GetSeed()
00092 {
00093   uint32 seed;
00094   int fd,n;
00095    
00096   fd = open( "/dev/random", O_RDONLY ); 
00097   if( fd < 0 )
00098   {
00099     cout << "GetSeed: Couldn't open \"/dev/random\"\n";
00100     return (uint32)time(0);
00101   } 
00102   n = read( fd, &seed, sizeof(seed) );
00103   if( n != sizeof(seed) )
00104   {
00105     close(fd);
00106 
00107     cout << "GetSeed: Couldn't read " << sizeof(seed) << "bytes\n";
00108     return (uint32)time(0);
00109   }  
00110 
00111   close(fd);
00112   return seed;
00113 }
00114 
00115 /*------------------------------------------------------------------
00116   Function for generating pseudo random numbers with DES crypto
00117 ------------------------------------------------------------------*/
00118 
00119 GULAPI void PseudoDes( uint32 *lword, uint32 *irword )
00120 {
00121   static uint32 c1[4] = {
00122     0xbaa96887L, 0x1e17d32cL, 0x03bcdc3cL, 0x0f33d1b2L };
00123   static uint32 c2[4] = {
00124     0x4b0f3b58L, 0xe874f0c3L, 0x6955c5a6L, 0x55a7ca46L };
00125     
00126   uint32 i,ia,ib,iswap,itmph=0,itmpl=0;
00127 
00128   for( i = 0; i < 4; i++ )
00129   {
00130     ia = (iswap=(*irword)) ^ c1[i];
00131     itmpl = ia & 0xffff;
00132     itmph = ia >> 16;
00133     ib = itmpl*itmpl + ~(itmph*itmph);
00134     ia = (ib>>16) | ((ib&0xffff)<<16);
00135     *irword = (*lword) ^ ((ia ^ c2[i]) + itmpl*itmph);
00136     *lword = iswap;
00137   }
00138 }
00139 
00140 }
00141 

Generated on Mon Jan 21 04:17:34 2002 for GUL 0.6 - Geometry Utility Library by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001