浏览代码

Revert "Properly setup seed in RNG"

This reverts commit 1dd72dca4588aba54ca9ef8233749fb97f2f5ff9.
As pointed out in #27171, it would break compatibility with 3.1.0.
Rémi Verschelde 6 年之前
父节点
当前提交
b5d9099626
共有 4 个文件被更改,包括 5 次插入15 次删除
  1. 2 1
      core/math/random_pcg.cpp
  2. 3 3
      core/math/random_pcg.h
  3. 0 10
      thirdparty/misc/pcg.cpp
  4. 0 1
      thirdparty/misc/pcg.h

+ 2 - 1
core/math/random_pcg.cpp

@@ -34,7 +34,8 @@
 
 RandomPCG::RandomPCG(uint64_t p_seed, uint64_t p_inc) :
 		pcg(),
-		current_inc(p_inc) {
+		current_seed(DEFAULT_SEED) {
+	pcg.inc = p_inc;
 	seed(p_seed);
 }
 

+ 3 - 3
core/math/random_pcg.h

@@ -38,18 +38,18 @@
 class RandomPCG {
 	pcg32_random_t pcg;
 	uint64_t current_seed; // seed with this to get the same state
-	uint64_t current_inc;
 
 public:
 	static const uint64_t DEFAULT_SEED = 12047754176567800795U;
 	static const uint64_t DEFAULT_INC = PCG_DEFAULT_INC_64;
 	static const uint64_t RANDOM_MAX = 0xFFFFFFFF;
 
-	RandomPCG(uint64_t p_seed = DEFAULT_SEED, uint64_t p_inc = DEFAULT_INC);
+	RandomPCG(uint64_t p_seed = DEFAULT_SEED, uint64_t p_inc = PCG_DEFAULT_INC_64);
 
 	_FORCE_INLINE_ void seed(uint64_t p_seed) {
 		current_seed = p_seed;
-		pcg32_srandom_r(&pcg, current_seed, current_inc);
+		pcg.state = p_seed;
+		pcg32_random_r(&pcg); // Force changing internal state to avoid initial 0
 	}
 	_FORCE_INLINE_ uint64_t get_seed() { return current_seed; }
 

+ 0 - 10
thirdparty/misc/pcg.cpp

@@ -13,13 +13,3 @@ uint32_t pcg32_random_r(pcg32_random_t* rng)
     uint32_t rot = oldstate >> 59u;
     return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
 }
-
-// Source from http://www.pcg-random.org/downloads/pcg-c-basic-0.9.zip
-void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq)
-{
-    rng->state = 0U;
-    rng->inc = (initseq << 1u) | 1u;
-    pcg32_random_r(rng);
-    rng->state += initstate;
-    pcg32_random_r(rng);
-}

+ 0 - 1
thirdparty/misc/pcg.h

@@ -10,6 +10,5 @@
 
 typedef struct { uint64_t state;  uint64_t inc; } pcg32_random_t;
 uint32_t pcg32_random_r(pcg32_random_t* rng);
-void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq);
 
 #endif // RANDOM_H