random.cpp

00001 #include "random.h"
00002 
00003 #include <stdlib.h>
00004 
00005 
00006 //use this first to seed the random number generator,
00007 //call this before any of the other functions
00008 Random::Random(int seedTime)
00009 {
00010     //ctor
00011   //  srand(seedTime);
00012 }
00013 
00014 Random::~Random()
00015 {
00016     //dtor
00017 }
00018 
00019 
00020 
00021 //generates a psuedo-random integer between 0 and 32767
00022 int Random::randint()
00023 {
00024     return rand();
00025 }
00026 
00027 //generates a psuedo-random integer between 0 and max
00028 int Random::randint(int max)
00029 {
00030     return (int) (max*rand()/(RAND_MAX+1.0));
00031 }
00032 
00033 //generates a psuedo-random integer between min and max
00034 int Random::randint(int min, int max)
00035 {
00036     if (min>max)
00037     {
00038         return max + (int) ((min-max+1)*rand()/(RAND_MAX+1.0));
00039     }
00040     else
00041     {
00042         return min + (int) ((max-min+1)*rand()/(RAND_MAX+1.0));
00043     }
00044 }
00045 
00046 //generates a psuedo-random float between 0.0 and 0.999...
00047 float Random::randfloat()
00048 {
00049     return rand()/((float)(RAND_MAX)+1);
00050 }
00051 
00052 //generates a psuedo-random float between 0.0 and max
00053 float Random::randfloat(float max)
00054 {
00055     return randfloat()*max;
00056 }
00057 
00058 //generates a psuedo-random float between min and max
00059 float Random::randfloat(float min, float max)
00060 {
00061     if (min>max)
00062     {
00063         return randfloat()*(min-max)+max;
00064     }
00065     else
00066     {
00067         return randfloat()*(max-min)+min;
00068     }
00069 }
00070 
00071 //generates a psuedo-random double between 0.0 and 0.999...
00072 double Random::randdouble()
00073 {
00074     return rand()/((double)(RAND_MAX)+1);
00075 }
00076 
00077 //generates a psuedo-random double between 0.0 and max
00078 double Random::randdouble(double max)
00079 {
00080     return randdouble()*max;
00081 }
00082 
00083 //generates a psuedo-random double between min and max
00084 double Random::randdouble(double min, double max)
00085 {
00086     if (min>max)
00087     {
00088         return randdouble()*(min-max)+max;
00089     }
00090     else
00091     {
00092         return randdouble()*(max-min)+min;
00093     }
00094 }
00095 

Generated on Sun Dec 2 17:09:58 2007 for Swarm by  doxygen 1.4.6-NO