replace rand
This commit is contained in:
@@ -11,7 +11,31 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
unsigned long randseed = time(NULL);
|
int randseed = time(NULL);
|
||||||
|
|
||||||
|
// From "Numerical Recipes in C".
|
||||||
|
float RandomFloat( int &seed )
|
||||||
|
{
|
||||||
|
const int MASK = 123459876;
|
||||||
|
seed ^= MASK;
|
||||||
|
|
||||||
|
const int IA = 16807;
|
||||||
|
const int IM = 2147483647;
|
||||||
|
|
||||||
|
const int IQ = 127773;
|
||||||
|
const int IR = 2836;
|
||||||
|
|
||||||
|
long k = seed / IQ;
|
||||||
|
seed = IA*(seed-k*IQ)-IR*k;
|
||||||
|
if( seed < 0 )
|
||||||
|
seed += IM;
|
||||||
|
|
||||||
|
const float AM = .999999f / IM;
|
||||||
|
float ans = AM * seed;
|
||||||
|
|
||||||
|
seed ^= MASK;
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
RandomGen::RandomGen( unsigned long seed_ )
|
RandomGen::RandomGen( unsigned long seed_ )
|
||||||
{
|
{
|
||||||
@@ -22,8 +46,7 @@ RandomGen::RandomGen( unsigned long seed_ )
|
|||||||
|
|
||||||
int RandomGen::operator() ( int maximum )
|
int RandomGen::operator() ( int maximum )
|
||||||
{
|
{
|
||||||
seed = 1664525L * seed + 1013904223L;
|
return int(RandomFloat( seed ) * maximum);
|
||||||
return (seed >> 2) % maximum;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -112,40 +112,31 @@ inline uint32_t Swap24LE( uint32_t n ) { return Swap24( n ); }
|
|||||||
inline uint16_t Swap16LE( uint16_t n ) { return Swap16( n ); }
|
inline uint16_t Swap16LE( uint16_t n ) { return Swap16( n ); }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Fast random number generators
|
extern int randseed;
|
||||||
// Taken from "Numerical Recipes in C"
|
|
||||||
|
|
||||||
extern unsigned long randseed;
|
float RandomFloat( int &seed );
|
||||||
|
|
||||||
inline unsigned long Random()
|
|
||||||
{
|
|
||||||
randseed = 1664525L * randseed + 1013904223L;
|
|
||||||
return randseed;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline float RandomFloat()
|
inline float RandomFloat()
|
||||||
{
|
{
|
||||||
randseed = 1664525L * randseed + 1013904223L;
|
return RandomFloat( randseed );
|
||||||
unsigned long itemp = 0x3f800000 | (0x007fffff & randseed);
|
|
||||||
return (*(float *)&itemp) - 1.0f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a float between dLow and dHigh inclusive
|
// Returns a float between dLow and dHigh inclusive
|
||||||
inline float RandomFloat(float fLow, float fHigh)
|
inline float RandomFloat(float fLow, float fHigh)
|
||||||
{
|
{
|
||||||
return RandomFloat() * (fHigh - fLow) + fLow;
|
return SCALE( RandomFloat(), 0.0f, 1.0f, fLow, fHigh );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an integer between nLow and nHigh inclusive
|
// Returns an integer between nLow and nHigh inclusive
|
||||||
inline int RandomInt(int nLow, int nHigh)
|
inline int RandomInt(int nLow, int nHigh)
|
||||||
{
|
{
|
||||||
return ((Random() >> 2) % (nHigh - nLow + 1)) + nLow;
|
return int( RandomFloat() * (nHigh - nLow + 1) + nLow );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Alternative: */
|
/* Alternative: */
|
||||||
class RandomGen
|
class RandomGen
|
||||||
{
|
{
|
||||||
unsigned long seed;
|
int seed;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RandomGen( unsigned long seed = 0 );
|
RandomGen( unsigned long seed = 0 );
|
||||||
@@ -156,7 +147,7 @@ public:
|
|||||||
// Simple function for generating random numbers
|
// Simple function for generating random numbers
|
||||||
inline float randomf( const float low=-1.0f, const float high=1.0f )
|
inline float randomf( const float low=-1.0f, const float high=1.0f )
|
||||||
{
|
{
|
||||||
return low + ( high - low ) * ( (float)rand() ) / RAND_MAX;
|
return RandomFloat( low, high );
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float froundf( const float f, const float fRoundInterval )
|
inline float froundf( const float f, const float fRoundInterval )
|
||||||
|
|||||||
Reference in New Issue
Block a user