code wiki / _hdl_build / nx_viz_random.nx

nx_viz_random.nx source

↩ module page · 29 lines · 1338 B

1// nx_viz_random.nx -- the DATA-RANDOM layer of the sovereign Nishi viz library (the d3-random core, bits-up). 2// A deterministic seeded PRNG (LCG, MMIX constants; HIGH bits only -- low bits of an LCG are low quality) + 3// uniform + central-limit gaussian (~N(0,1000^2)). For jitter / sampling / beeswarm / violin. license_tier: ORIGINAL 4import "nx_syscalls.nx" 5const K_MAGIC_6364136223846793005: i64 = 6364136223846793005 6const K_MAGIC_1442695040888963407: i64 = 1442695040888963407 7const K_MAGIC_2147483647: i64 = 2147483647 8const K_MAGIC_6000: i64 = 6000 9 10// advance the LCG state in place; returns the new 64-bit state. 11func vr_next(statep: *i64) -> i64 { 12 let ns: i64 = statep[0] * K_MAGIC_6364136223846793005 + K_MAGIC_1442695040888963407 13 statep[0] = ns 14 return ns 15} 16// uniform in [0,n) from the HIGH 31 bits (avoids LCG low-bit correlation). 17func vr_uniform(statep: *i64, n: i64) -> i64 { 18 if n <= 0 { return 0 } 19 let r: i64 = vr_next(statep) 20 let hi: i64 = (r >> 33) & K_MAGIC_2147483647 21 return hi % n 22} 23// ~N(0, 1000^2) via central limit: sum of 12 uniforms[0,1000] minus 6000 (std ~1000 = 1.0). 24func vr_gaussian(statep: *i64) -> i64 { 25 var sum: i64 = 0; var i: i64 = 0 26 while i < 12 { sum = sum + vr_uniform(statep, 1000); i = i + 1 } 27 return sum - K_MAGIC_6000 28} 29func main() -> i64 { return 0 }