Created
April 11, 2012 19:22
-
-
Save jsanders/2361647 to your computer and use it in GitHub Desktop.
Very simple linear congruential PRNG in dasm
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
; Puts "random" numbers in X, Y, and Z | |
SET A, 0x1232 | |
JSR rand | |
SET X, A | |
JSR rand | |
SET Y, A | |
JSR rand | |
SET Z, A | |
SET PC, break | |
:rand MUL A, 0x4e6d | |
ADD A, 0x3039 | |
SET PC, POP | |
; Non-standard, but works in the emulator at http://mappum.github.com/DCPU-16/ | |
:break BRK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A pretty dump little PRNG, that doesn't even use the real LCG algorithm (http://en.wikipedia.org/wiki/Linear_congruential_generator) because it ignores overflow on the MUL, so it's really
Xn+1 = (a*Xn (mod 2^16)) + c
, which is obviously pretty different thanXn+1 = (a*Xn + c) (mod 2^16)
.Next steps: do the algorithm right, seed from keyboard input, maybe create an
itoa
so that I can print the random numbers, maybe figure out how to store the current seed without assuming it is in A (implies either well-known memory location or some sort of memory management, the former sounds easier, the latter sounds like more fun).