chaos.cpp

Go to the documentation of this file.
00001 #ifndef CHAOS_IMPLEMENTATION_FILE
00002 #define CHAOS_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : chaos                                                             *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 //#define DEBUG_CHAOS
00019   // uncomment for debugging version.
00020 
00021 #include "chaos.h"
00022 #include "log_base.h"
00023 #include "portable.h"
00024 
00025 #include <stdlib.h>
00026 #include <time.h>
00027 
00028 #undef LOG
00029 #ifdef DEBUG_CHAOS
00030   #define LOG(s) STAMPED_EMERGENCY_LOG(program_wide_logger(), s);
00031 #else
00032   #define LOG(s) {}
00033 #endif
00034 
00035 chaos::chaos() {}
00036 
00037 void chaos::retrain()
00038 {
00039   time_t time_num;
00040   time(&time_num);
00041   // "t" is an ugly pointer into system data that contains the time nicely
00042   // broken up into segments.  the pointer cannot be freed!
00043   tm *t = localtime(&time_num);
00044   int add_in_milliseconds = portable::system_uptime();
00045   // create a good random number seed from the time.
00046   LOG(isprintf("day %d hour %d min %d sec %d", t->tm_mday, t->tm_hour,
00047       t->tm_min, t->tm_sec));
00048   unsigned int temp = (t->tm_sec + 60 * t->tm_mday + 60 * 31 * t->tm_hour
00049     + 24 * 60 * 31 * t->tm_min) ^ add_in_milliseconds;
00050   // initialize random generator.
00051   srand(int(temp));
00052 }
00053 
00054 // gets a 32 bit integer from the 15 bit random generator.  it's 15 bit because
00055 // rand() only generates numbers up to the MAX_RAND, which in the visual c++
00056 // case is limited to 0x7FFF.  so, we let the first random number generated
00057 // serve as the upper two bytes and we shift another one over a bit to cover
00058 // the whole range of the third and fourth byte, except for that last bit, 
00059 // which is added in as a binary random value.
00060 #define GET_RAND \
00061   u_int ranval = (u_int(rand()) << 16) + (u_int(rand()) << 1) \
00062        + (u_int(rand()) % 2)
00063 
00064 int chaos::inclusive(int low, int high) const
00065 {
00066   if (high < low) return low;
00067   unsigned int range = high - low + 1;
00068   GET_RAND;
00069   int adjusted = ranval % range + low;
00070   return adjusted;
00071 }
00072 
00073 int chaos::exclusive(int low, int high) const
00074 {
00075   if (high < low) return low + 1;
00076   unsigned int range = high - low - 1;
00077   GET_RAND;
00078   int adjusted = ranval % range + low + 1;
00079   return adjusted;
00080 }
00081 
00082 
00083 
00084 #endif //CHAOS_IMPLEMENTATION_FILE
00085 

Generated on Fri Oct 10 04:28:40 2008 for HOOPLE Libraries by  doxygen 1.5.1