From f0f340dec7821cee103ab9267ef941a917ef4dc4 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 18 Nov 2018 23:31:17 +0800 Subject: =?UTF-8?q?*=E7=9B=AE=E5=BD=95=E6=94=B9=E4=B8=BA=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/math/je_random.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/libjin/math/je_random.cpp (limited to 'src/libjin/math/je_random.cpp') diff --git a/src/libjin/math/je_random.cpp b/src/libjin/math/je_random.cpp new file mode 100644 index 0000000..216fd79 --- /dev/null +++ b/src/libjin/math/je_random.cpp @@ -0,0 +1,52 @@ +#include +#include "je_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) + { + uint32 a = pow(10.f, ac); + uint32 n = rand(min*a, max*a); + return (float)n / a; + } + + } +} \ No newline at end of file -- cgit v1.1-26-g67d0