Last active
June 1, 2021 19:40
-
-
Save going-digital/3e5d0eb8b20fc3aecac76791c8698b7c to your computer and use it in GitHub Desktop.
Pseudorandom numbers in WebAssembly
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
;; 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