mirror of
https://github.com/love2d/love.git
synced 2026-08-15 07:41:11 +02:00
Added love.math.newRandomGenerator. Returns a RNG object with its own seed and random methods.
--HG-- branch : math-randobject
This commit is contained in:
@@ -18,7 +18,7 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#include "modules/math/MathModule.h"
|
||||
#include "MathModule.h"
|
||||
#include "common/math.h"
|
||||
|
||||
#include <cmath>
|
||||
@@ -81,60 +81,22 @@ namespace math
|
||||
|
||||
Math Math::instance;
|
||||
|
||||
// 64 bit Xorshift implementation taken from the end of Sec. 3 (page 4) in
|
||||
// George Marsaglia, "Xorshift RNGs", Journal of Statistical Software, Vol.8 (Issue 14), 2003
|
||||
Math::Math()
|
||||
: last_randnormal(numeric_limits<double>::infinity())
|
||||
: rng(newRandomGenerator())
|
||||
{
|
||||
// because it is too big for some compilers to handle ... if you know what
|
||||
// i mean
|
||||
union
|
||||
{
|
||||
uint64 b64;
|
||||
struct
|
||||
{
|
||||
uint32 a;
|
||||
uint32 b;
|
||||
} b32;
|
||||
} converter;
|
||||
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
converter.b32.a = 0x0139408D;
|
||||
converter.b32.b = 0xCBBF7A44;
|
||||
#else
|
||||
converter.b32.b = 0x0139408D;
|
||||
converter.b32.a = 0xCBBF7A44;
|
||||
#endif
|
||||
rng_state = converter.b64;
|
||||
|
||||
// prevent the runtime from free()-ing this
|
||||
retain();
|
||||
}
|
||||
|
||||
uint64 Math::rand()
|
||||
Math::~Math()
|
||||
{
|
||||
rng_state ^= (rng_state << 13);
|
||||
rng_state ^= (rng_state >> 7);
|
||||
rng_state ^= (rng_state << 17);
|
||||
return rng_state;
|
||||
if (rng)
|
||||
rng->release();
|
||||
}
|
||||
|
||||
// Box–Muller transform
|
||||
double Math::randnormal(double stddev)
|
||||
RandomGenerator *Math::newRandomGenerator()
|
||||
{
|
||||
// use cached number if possible
|
||||
if (last_randnormal != numeric_limits<double>::infinity())
|
||||
{
|
||||
double r = last_randnormal;
|
||||
last_randnormal = numeric_limits<double>::infinity();
|
||||
return r * stddev;
|
||||
}
|
||||
|
||||
double r = sqrt(-2.0 * log(1. - random()));
|
||||
double phi = 2.0 * LOVE_M_PI * (1. - random());
|
||||
|
||||
last_randnormal = r * cos(phi);
|
||||
return r * sin(phi) * stddev;
|
||||
return new RandomGenerator();
|
||||
}
|
||||
|
||||
vector<Triangle> Math::triangulate(const vector<vertex> &polygon)
|
||||
|
||||
Reference in New Issue
Block a user