Skip to content

Instantly share code, notes, and snippets.

@rfinnie
Last active April 19, 2019 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfinnie/cbf3dde3353e7da53d92d9aadeeb8773 to your computer and use it in GitHub Desktop.
Save rfinnie/cbf3dde3353e7da53d92d9aadeeb8773 to your computer and use it in GitHub Desktop.
# smwrand
# Ryan Finnie <ryan@finnie.org>
# 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