mirror of
https://github.com/love2d/love.git
synced 2026-08-15 15:51:12 +02:00
Changed love.math.randomnormal([mean], stddev) to love.math.randomNormal(stddev, [mean]);
Changed love.math.randomseed(seed) to love.math.setRandomState(state) and love.math.setRandomState(low, high). Added low, high = love.math.getRandomState(). love.math.setRandomState(state) can set the random state to an integer of up to 53 bits, love.math.setRandomState(low, high) can set the random state to a 64 bit integer by using the first argument as the lower 32 bits and the second argument as the higher 32 bits. This allows for reliable setting and saving of the state of a RNG. --HG-- branch : RandomGenerator-2
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#define LOVE_MATH_RANDOM_GENERATOR_H
|
||||
|
||||
// LOVE
|
||||
#include "common/config.h"
|
||||
#include "common/math.h"
|
||||
#include "common/int.h"
|
||||
#include "common/Object.h"
|
||||
@@ -38,19 +39,56 @@ class RandomGenerator : public Object
|
||||
{
|
||||
public:
|
||||
|
||||
RandomGenerator();
|
||||
union State
|
||||
{
|
||||
uint64 b64;
|
||||
struct
|
||||
{
|
||||
uint32 a;
|
||||
uint32 b;
|
||||
} b32;
|
||||
};
|
||||
|
||||
virtual ~RandomGenerator() {};
|
||||
RandomGenerator();
|
||||
virtual ~RandomGenerator() {}
|
||||
|
||||
/**
|
||||
* Set pseudo random seed.
|
||||
* Set pseudo-random state.
|
||||
* It's up to the implementation how to use this.
|
||||
*
|
||||
* @param seed The random seed.
|
||||
**/
|
||||
inline void randomseed(uint64 seed)
|
||||
inline void setState(State state)
|
||||
{
|
||||
rng_state = seed;
|
||||
rng_state = state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separately set the low and high parts of the pseudo-random state.
|
||||
**/
|
||||
inline void setState(uint32 low, uint32 high)
|
||||
{
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
rng_state.b32.a = high;
|
||||
rng_state.b32.b = low;
|
||||
#else
|
||||
rng_state.b32.b = high;
|
||||
rng_state.b32.a = low;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline State getState() const
|
||||
{
|
||||
return rng_state;
|
||||
}
|
||||
|
||||
inline void getState(uint32 &low, uint32 &high) const
|
||||
{
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
high = rng_state.b32.a;
|
||||
low = rng_state.b32.b;
|
||||
#else
|
||||
high = rng_state.b32.b;
|
||||
low = rng_state.b32.a;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,11 +134,11 @@ public:
|
||||
* @param stddev Standard deviation of the distribution.
|
||||
* @return Normally distributed random number with mean 0 and variance (stddev)².
|
||||
**/
|
||||
double randomnormal(double stddev);
|
||||
double randomNormal(double stddev);
|
||||
|
||||
private:
|
||||
|
||||
uint64 rng_state;
|
||||
State rng_state;
|
||||
double last_randomnormal;
|
||||
|
||||
}; // RandomGenerator
|
||||
|
||||
Reference in New Issue
Block a user