diff options
| author | daoge_cmd <3523206925@qq.com> | 2026-03-01 12:16:08 +0800 |
|---|---|---|
| committer | daoge_cmd <3523206925@qq.com> | 2026-03-01 12:16:08 +0800 |
| commit | b691c43c44ff180d10e7d4a9afc83b98551ff586 (patch) | |
| tree | 3e9849222cbc6ba49f2f1fc6e5fe7179632c7390 /Minecraft.World/Random.cpp | |
| parent | def8cb415354ac390b7e89052a50605285f1aca9 (diff) | |
Initial commit
Diffstat (limited to 'Minecraft.World/Random.cpp')
| -rw-r--r-- | Minecraft.World/Random.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/Minecraft.World/Random.cpp b/Minecraft.World/Random.cpp new file mode 100644 index 00000000..9176bf38 --- /dev/null +++ b/Minecraft.World/Random.cpp @@ -0,0 +1,106 @@ +#include "stdafx.h" +#include "Random.h" +#include "System.h" + +Random::Random() +{ + // 4J - jave now uses the system nanosecond counter added to a "seedUniquifier" to get an initial seed. Our nanosecond timer is actually only millisecond accuate, so + // use QueryPerformanceCounter here instead + __int64 seed; + QueryPerformanceCounter((LARGE_INTEGER *)&seed); + seed += 8682522807148012LL; + + setSeed(seed); +} + +Random::Random(__int64 seed) +{ + setSeed(seed); +} + +void Random::setSeed(__int64 s) +{ + this->seed = (s ^ 0x5DEECE66DLL) & ((1LL << 48) - 1); + haveNextNextGaussian = false; +} + +int Random::next(int bits) +{ + seed = (seed * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1); + return (int)(seed >> (48 - bits)); +} + +void Random::nextBytes(byte *bytes, unsigned int count) +{ + for(unsigned int i = 0; i < count; i++ ) + { + bytes[i] = (byte)next(8); + } +} + +double Random::nextDouble() +{ + + return (((__int64)next(26) << 27) + next(27)) + / (double)(1LL << 53); +} + +double Random::nextGaussian() +{ + if (haveNextNextGaussian) + { + haveNextNextGaussian = false; + return nextNextGaussian; + } + else + { + double v1, v2, s; + do + { + v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0 + v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 + s = v1 * v1 + v2 * v2; + } while (s >= 1 || s == 0); + double multiplier = sqrt(-2 * log(s)/s); + nextNextGaussian = v2 * multiplier; + haveNextNextGaussian = true; + return v1 * multiplier; + } +} + +int Random::nextInt() +{ + return next(32); +} + +int Random::nextInt(int n) +{ + assert (n>0); + + + if ((n & -n) == n) // i.e., n is a power of 2 + return (int)(((__int64)next(31) * n) >> 31); // 4J Stu - Made __int64 instead of long + + int bits, val; + do + { + bits = next(31); + val = bits % n; + } while(bits - val + (n-1) < 0); + return val; +} + +float Random::nextFloat() +{ + return next(24) / ((float)(1 << 24)); +} + +__int64 Random::nextLong() +{ + return ((__int64)next(32) << 32) + next(32); +} + +bool Random::nextBoolean() +{ + return next(1) != 0; +}
\ No newline at end of file |
