Created
June 28, 2023 09:01
-
-
Save tkgstrator/55ae6da16977b15a00417978d3406d1f to your computer and use it in GitHub Desktop.
RNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main(void) { | |
unsigned long long RNG1 = 0x12345; | |
unsigned long long RNG2 = 0x12345; | |
unsigned int highorder; | |
unsigned int loworder; | |
for (int i = 0x0; i < 0x10; i++) { | |
highorder = (RNG1 * 0x6C078965) >> 32; | |
loworder = (RNG1 * 0x6C078965 << 32) / 0xFFFFFFFF; | |
highorder = 0x6C078965 * RNG2 + highorder; | |
RNG2 = (0x5D588B65 * RNG1 + highorder) & 0xFFFFFFFF; | |
RNG1 = (loworder + 0x269EC3) & 0xFFFFFFFF; | |
printf("%08llX %08llX\n", RNG1, RNG2); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NSRandom: | |
def __init__(self, mRandom1, mRandom2): | |
self._mRandom1 = mRandom1 | |
self._mRandom2 = mRandom2 | |
def getU32(self) -> (int, int): | |
# 計算用の64ビット整数 | |
mSeed = (self._mRandom1 * 0x6C078965) & 0xFFFFFFFFFFFFFFFF | |
# 上位32ビット, 下位32ビット | |
mSeed1, mSeed2 = mSeed >> 0x20, mSeed & 0xFFFFFFFF | |
# 上位32ビットを再計算 | |
mSeed1 = (0x6C078965 * self._mRandom2 + mSeed1) & 0xFFFFFFFF | |
# 新しい乱数としてセットする | |
self._mRandom2 = (0x5D588B65 * self._mRandom1 + mSeed1) & 0xFFFFFFFF | |
self._mRandom1 = (mSeed2 + 0x00269EC3) & 0xFFFFFFFF | |
return self._mRandom1, self._mRandom2 | |
if __name__=='__main__': | |
rnd = NSRandom(0x12345, 0x12345) | |
for count in range(0, 10): | |
rnd1, rnd2 = rnd.getU32() | |
print('%08X' % rnd1, '%08X' % rnd2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment