# Changeset 2520Tweet

Ignore:
Timestamp:
Feb 27, 2013, 10:25:47 AM (8 years ago)
Message:

math: replace RandF() with a more generic rand() template function
that lets us draw integer random values too.

Location:
trunk
Files:
9 edited

Unmodified
Removed
• ## trunk/src/easymesh/easymesh.cpp

 r2516 { if(Welded[j] == -1) m_vert[i].m_coord *= 1.0f + RandF(r); m_vert[i].m_coord *= 1.0f + rand(r); else m_vert[i].m_coord = m_vert[Welded[j]].m_coord;
• ## trunk/src/numeric.h

 r2444 // Lol Engine // // Copyright: (c) 2010-2011 Sam Hocevar // Copyright: (c) 2010-2013 Sam Hocevar //   This program is free software; you can redistribute it and/or //   modify it under the terms of the Do What The Fuck You Want To { /* Random float value */ static inline float RandF() /* Random number generators */ template static inline T rand(); template static inline T rand(T a); template static inline T rand(T a, T b); /* One-value random number generators */ template static inline T rand(T a) { using namespace std; return (float)std::rand() / RAND_MAX; return rand() % a; } static inline float RandF(float val) template<> inline half rand(half a) { return a * std::rand() / RAND_MAX; } template<> inline float rand(float a) { return a * std::rand() / RAND_MAX; } template<> inline double rand(double a) { return a * std::rand() / RAND_MAX; } template<> inline ldouble rand(ldouble a) { return a * std::rand() / RAND_MAX; } /* Two-value random number generator -- no need for specialisation */ template static inline T rand(T a, T b) { return RandF() * val; return a + rand(b - a); } static inline float RandF(float min, float max) /* Default random number generator */ template static inline T rand() { return min + RandF() * (max - min); switch (sizeof(T)) { case 1: return static_cast(std::rand() & 0xff); case 2: if (RAND_MAX >= 0xffff) return static_cast(std::rand()); else return static_cast((std::rand() << 8) ^ std::rand()); case 4: if (RAND_MAX >= 0xffff) return static_cast((std::rand() << 16) ^ std::rand()); else return static_cast((std::rand() << 24) ^ (std::rand() << 16) ^ (std::rand() << 8) ^ std::rand()); case 8: if (RAND_MAX >= 0xffff) return static_cast(((uint64_t)std::rand() << 48) ^ ((uint64_t)std::rand() << 32) ^ ((uint64_t)std::rand() << 16) ^ ((uint64_t)std::rand())); else return static_cast(((uint64_t)std::rand() << 56) ^ ((uint64_t)std::rand() << 48) ^ ((uint64_t)std::rand() << 40) ^ ((uint64_t)std::rand() << 32) ^ ((uint64_t)std::rand() << 24) ^ ((uint64_t)std::rand() << 16) ^ ((uint64_t)std::rand() << 8) ^ ((uint64_t)std::rand())); default: ASSERT(false, "rand() doesn’t support types of size %d\n", (int)sizeof(T)); } } template<> inline half rand() { return rand(1.f); } template<> inline float rand() { return rand(1.f); } template<> inline double rand() { return rand(1.0); } template<> inline ldouble rand() { return rand(1.0); } /* Next power of two. */
• ## trunk/src/scene.cpp

 r2506 { Tile tmp = data->m_tiles[i]; int j = std::rand() % data->m_tiles.Count(); int j = rand() % data->m_tiles.Count(); data->m_tiles[i] = data->m_tiles[j]; data->m_tiles[j] = tmp;
• ## trunk/src/ticker.cpp

 r2506 /* Ensure some randomness */ (void)std::rand(); rand(); /* If recording with fixed framerate, set deltatime to a fixed value */
• ## trunk/test/benchmark/half.cpp

 r2183 case 1: for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++) ph[i] = half::makebits(rand()); ph[i] = half::makebits(rand()); break; case 2: for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++) ph[i] = RandF(-2.0f, 2.0f); ph[i] = rand(-2.0f, 2.0f); break; }
• ## trunk/test/benchmark/trig.cpp

 r2216 case 1: for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) pf[i] = RandF(-1e5f, 1e5f); pf[i] = rand(-1e5f, 1e5f); break; case 2: for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) pf[i] = RandF(-M_PI, M_PI); pf[i] = rand(-M_PI, M_PI); break; case 3: for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) pf[i] = RandF(-1e-2f, 1e-2f); pf[i] = rand(-1e-2f, 1e-2f); break; }
• ## trunk/test/benchmark/vector.cpp

 r2183 for (int j = 0; j < 4; j++) for (int k = 0; k < 4; k++) pm[i][j][k] = RandF(-2.0f, 2.0f); pm[i][j][k] = rand(-2.0f, 2.0f); break; }
• ## trunk/test/physicobject.h

 r2451 case 0: { RandValue = (int)(lol::RandF() * (SphereLimit - 1)); RandValue = rand(SphereLimit); break; } case 1: { RandValue = SphereLimit + (int)(lol::RandF() * ((ConeLimit - SphereLimit) - 1)); RandValue = rand(SphereLimit, ConeLimit); break; } case 2: { RandValue = ConeLimit + (int)(lol::RandF() * ((CylLimit - ConeLimit) - 1)); RandValue = rand(ConeLimit, CylLimit); break; } case 3: { RandValue = CylLimit + (int)(lol::RandF() * ((CapsLimit - CylLimit) - 1)); RandValue = rand(CylLimit, CapsLimit); break; } case 4: { RandValue = CapsLimit + (int)(lol::RandF() * ((MeshRand.Count() - CapsLimit) - 1)); RandValue = rand(CapsLimit, MeshRand.Count()); break; } default: { RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1)); RandValue = rand(MeshRand.Count()); } }
• ## trunk/test/unit/build.cpp

 r2216 double x, y; y = x = 1.0 + RandF(0.1f, 0.2f); y = x = 1.0 + rand(0.1f, 0.2f); y += 4503599627370496.0; /* The compiler should optimise this away */ double x, y; y = x = 1.0 + RandF(0.1f, 0.2f); y = x = 1.0 + rand(0.1f, 0.2f); y += 4503599627370496.0; FP_USE(y);
Note: See TracChangeset for help on using the changeset viewer.