00001 #ifndef CHAOS_IMPLEMENTATION_FILE
00002 #define CHAOS_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00042
00043 tm *t = localtime(&time_num);
00044 int add_in_milliseconds = portable::system_uptime();
00045
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
00051 srand(int(temp));
00052 }
00053
00054
00055
00056
00057
00058
00059
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