lugre_random.cpp

Go to the documentation of this file.
00001 #include "lugre_prefix.h"
00002 #include "lugre_random.h"
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 
00008 #ifdef WIN32
00009 #define snprintf _snprintf
00010 #endif
00011 
00012 #define RANDOM_FLOAT_PRECISION  0xFFFFFF
00013 
00014 unsigned int NextValue(unsigned int last){
00015     //printf("cRandom::NextValue(%d)\n",last);
00016     
00017     while(last < 0xFFFF){
00018         last = last * last + last + 1;
00019         //printf("last=%d\n",last);
00020     }
00021     
00022     static char buf[128+1];
00023     char *p = buf;
00024     snprintf(buf,128,"%d",last);
00025     //printf("buf=%s\n",buf);
00026     int len = strlen(buf);
00027     p += len / 4;
00028     *(p + len / 2 + 1) = 0;
00029     unsigned int v = atoi(p);
00030     v = v * v;
00031     //printf("-> %d\n",v);
00032     return v;
00033 }
00034 
00035 namespace Lugre {
00036     
00037 cRandom::cRandom                        (unsigned int seed){
00038     miSeed = seed;
00039     miLast = miSeed * miSeed;
00040 }
00041     
00042 unsigned int    cRandom::GetInt     (unsigned int max){
00043     return GetInt(1,max);
00044 }
00045 
00046 unsigned int    cRandom::GetInt     (unsigned int min, unsigned int max){
00047     miLast = NextValue(miLast);
00048     return min + (miLast % (max - min + 1));
00049 }
00050 
00051 float           cRandom::GetFloat   (){
00052     return (float)GetInt(0,RANDOM_FLOAT_PRECISION) / (float)RANDOM_FLOAT_PRECISION;
00053 }
00054 
00055 };

Generated on Wed May 23 06:00:15 2012 for cpp by  doxygen 1.5.6