Last active
April 19, 2019 05:26
-
-
Save rfinnie/cbf3dde3353e7da53d92d9aadeeb8773 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# smwrand | |
# Ryan Finnie <[email protected]> | |
# Based on deconstruction by Retro Game Mechanics Explained | |
# https://www.youtube.com/watch?v=q15yNrJHOak | |
class SMWRand: | |
seed_1 = 0 | |
seed_2 = 0 | |
def _rand(self): | |
self.seed_1 = (self.seed_1 + (self.seed_1 << 2) + 1) & 0xff | |
self.seed_2 = ((self.seed_2 << 1) + int((self.seed_2 & 0x90) in (0x90, 0))) & 0xff | |
return self.seed_1 ^ self.seed_2 | |
def rand(self): | |
output_2 = self._rand() | |
output_1 = self._rand() | |
return (output_1, output_2) | |
if __name__ == '__main__': | |
smwrand = SMWRand() | |
for i in range(20): | |
r = smwrand.rand() | |
print('Seed (post-output): 0x{:02x} 0x{:02x}, output: 0x{:02x} 0x{:02x}'.format( | |
smwrand.seed_1, smwrand.seed_2, r[0], r[1], | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment