mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-05 10:36:17 +02:00
Add Mersenne Twister random number generator
This commit is contained in:
1 parent
f6dd98570b
commit
5bcf145f88
2 files changed
+43
-3
No files matched your search
@@ -30,7 +30,6 @@ enum {
|
||||
PAD_BUTTON_LEFT_THUMB = 1 << 18, // Click left thumb stick on X360
|
||||
PAD_BUTTON_RIGHT_THUMB = 1 << 19, // Click right thumb stick on X360
|
||||
PAD_BUTTON_UNTHROTTLE = 1 << 20, // Click Tab to unthrottle
|
||||
|
||||
};
|
||||
|
||||
#ifndef MAX_POINTERS
|
||||
|
||||
+43
-2
@@ -1,4 +1,4 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
|
||||
#include "base/basictypes.h"
|
||||
|
||||
@@ -24,7 +24,48 @@ public:
|
||||
return (float)R32() / (float)(0xFFFFFFFF);
|
||||
}
|
||||
|
||||
private:
|
||||
// public for easy save/load. Yes a bit ugly but better than moving DoState into native.
|
||||
uint32 m_w;
|
||||
uint32 m_z;
|
||||
};
|
||||
|
||||
|
||||
// Data must consist only of the index and the twister array. This matches the PSP
|
||||
// MT context exactly.
|
||||
class MersenneTwister {
|
||||
public:
|
||||
MersenneTwister(uint32_t seed) : index_(0) {
|
||||
mt_[0] = seed;
|
||||
for (uint32_t i = 1; i < MT_SIZE; i++)
|
||||
mt_[i] = (1812433253 * (mt_[i - 1] ^ (mt_[i - 1] >> 30)) + i);
|
||||
}
|
||||
|
||||
uint32_t R32() {
|
||||
if (index_ == 0)
|
||||
gen();
|
||||
uint32_t y = mt_[index_];
|
||||
y ^= y >> 11;
|
||||
y ^= (y << 7) & 2636928640;
|
||||
y ^= (y << 15) & 4022730752;
|
||||
y ^= y >> 18;
|
||||
index_ = (index_ + 1) % MT_SIZE;
|
||||
return y;
|
||||
}
|
||||
|
||||
private:
|
||||
enum {
|
||||
MT_SIZE = 624,
|
||||
};
|
||||
|
||||
uint32_t index_;
|
||||
uint32_t mt_[MT_SIZE];
|
||||
|
||||
void gen() {
|
||||
for(uint32_t i = 0; i < MT_SIZE; i++){
|
||||
uint32_t y = (mt_[i] & 0x80000000) + (mt_[(i + 1) % MT_SIZE] & 0x80000000);
|
||||
mt_[i] = mt_[(i + 397) % MT_SIZE] ^ (y >> 1);
|
||||
if (y % 2) mt_[i] ^= 2567483615;
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user