aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/math/random.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-01-12 21:48:33 +0800
committerchai <chaifix@163.com>2019-01-12 21:48:33 +0800
commit8b00d67febf133e89f6a0bfabc41feed555dc4a9 (patch)
treefe48ef17c250afa40c2588300fcdb5920dba6951 /src/libjin/math/random.cpp
parenta907c39756ef6b368d06643afa491c49a9044a8e (diff)
*去掉文件前缀je_
Diffstat (limited to 'src/libjin/math/random.cpp')
-rw-r--r--src/libjin/math/random.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/libjin/math/random.cpp b/src/libjin/math/random.cpp
new file mode 100644
index 0000000..5f3b28c
--- /dev/null
+++ b/src/libjin/math/random.cpp
@@ -0,0 +1,54 @@
+#include <math.h>
+#include "math.h"
+#include "random.h"
+
+namespace JinEngine
+{
+ namespace Math
+ {
+
+ RandomGenerator::RandomGenerator(uint32 seed)
+ : mSeed(seed)
+ {
+ }
+
+ uint32 RandomGenerator::rand()
+ {
+ unsigned int next = mSeed;
+ uint32 result;
+
+ next *= 1103515245;
+ next += 12345;
+ result = (unsigned int)(next / 65536) % 2048;
+
+ next *= 1103515245;
+ next += 12345;
+ result <<= 10;
+ result ^= (unsigned int)(next / 65536) % 1024;
+
+ next *= 1103515245;
+ next += 12345;
+ result <<= 10;
+ result ^= (unsigned int)(next / 65536) % 1024;
+
+ mSeed = next;
+
+ return result;
+ }
+
+ uint32 RandomGenerator::rand(uint32 min, uint32 max)
+ {
+ uint32 n = rand();
+ return n % (max - min + 1) + min;
+ }
+
+ float RandomGenerator::randf(float min, float max, int ac)
+ {
+ float floor = 0, ceil = max + reverse<float>(min);
+ uint32 a = pow(10.f, ac);
+ uint32 n = rand(floor*a, ceil*a);
+ return (float)n / a + min;
+ }
+
+ }
+} \ No newline at end of file