Skip to content

Instantly share code, notes, and snippets.

@jsanders
Created April 11, 2012 19:22
Show Gist options
  • Save jsanders/2361647 to your computer and use it in GitHub Desktop.
Save jsanders/2361647 to your computer and use it in GitHub Desktop.
Very simple linear congruential PRNG in dasm
; 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
@jsanders
Copy link
Author

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 than Xn+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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment