Skip to content

Instantly share code, notes, and snippets.

@going-digital
Last active June 1, 2021 19:40
Show Gist options
  • Save going-digital/3e5d0eb8b20fc3aecac76791c8698b7c to your computer and use it in GitHub Desktop.
Save going-digital/3e5d0eb8b20fc3aecac76791c8698b7c to your computer and use it in GitHub Desktop.
Pseudorandom numbers in WebAssembly
;; From https://github.com/vsariola/sointu
;; Compiles to 19 bytes of WebAssembly code
;;
;; Returns pseudorandom numbers in the range 0 to 1
(module
(global $randseed (mut i32) (i32.const 1))
(export "rand" (func $rand))
(func $rand (result f32)
(set_global $randseed (i32.mul (get_global $randseed) (i32.const 16007)))
(f32.div (f32.convert_s/i32 (get_global $randseed)) (f32.const -2147483648))
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment