math 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/API.md +1605 -0
  2. package/LICENSE +21 -0
  3. package/README.md +197 -0
  4. package/dist/color/color.js +136 -0
  5. package/dist/color/color.js.map +1 -0
  6. package/dist/color/colorspace.js +43 -0
  7. package/dist/color/colorspace.js.map +1 -0
  8. package/dist/color/hsl.js +123 -0
  9. package/dist/color/hsl.js.map +1 -0
  10. package/dist/color/index.js +7 -0
  11. package/dist/color/index.js.map +1 -0
  12. package/dist/color/parse.js +276 -0
  13. package/dist/color/parse.js.map +1 -0
  14. package/dist/core/angle.js +43 -0
  15. package/dist/core/angle.js.map +1 -0
  16. package/dist/core/euler.js +212 -0
  17. package/dist/core/euler.js.map +1 -0
  18. package/dist/core/mat2.js +385 -0
  19. package/dist/core/mat2.js.map +1 -0
  20. package/dist/core/mat2d.js +422 -0
  21. package/dist/core/mat2d.js.map +1 -0
  22. package/dist/core/mat3.js +749 -0
  23. package/dist/core/mat3.js.map +1 -0
  24. package/dist/core/mat4.js +2197 -0
  25. package/dist/core/mat4.js.map +1 -0
  26. package/dist/core/polar.js +200 -0
  27. package/dist/core/polar.js.map +1 -0
  28. package/dist/core/quat.js +730 -0
  29. package/dist/core/quat.js.map +1 -0
  30. package/dist/core/quat2.js +883 -0
  31. package/dist/core/quat2.js.map +1 -0
  32. package/dist/core/scalar.js +112 -0
  33. package/dist/core/scalar.js.map +1 -0
  34. package/dist/core/spherical.js +248 -0
  35. package/dist/core/spherical.js.map +1 -0
  36. package/dist/core/vec2.js +597 -0
  37. package/dist/core/vec2.js.map +1 -0
  38. package/dist/core/vec3.js +916 -0
  39. package/dist/core/vec3.js.map +1 -0
  40. package/dist/core/vec4.js +573 -0
  41. package/dist/core/vec4.js.map +1 -0
  42. package/dist/geometry/circumcircle.js +39 -0
  43. package/dist/geometry/circumcircle.js.map +1 -0
  44. package/dist/geometry/index.js +6 -0
  45. package/dist/geometry/index.js.map +1 -0
  46. package/dist/geometry/polygon2-decompose.js +411 -0
  47. package/dist/geometry/polygon2-decompose.js.map +1 -0
  48. package/dist/geometry/polygon2-triangulate.js +222 -0
  49. package/dist/geometry/polygon2-triangulate.js.map +1 -0
  50. package/dist/geometry/quickhull2.js +123 -0
  51. package/dist/geometry/quickhull2.js.map +1 -0
  52. package/dist/geometry/quickhull3.js +605 -0
  53. package/dist/geometry/quickhull3.js.map +1 -0
  54. package/dist/ik/fabrik2.js +675 -0
  55. package/dist/ik/fabrik2.js.map +1 -0
  56. package/dist/ik/fabrik3.js +912 -0
  57. package/dist/ik/fabrik3.js.map +1 -0
  58. package/dist/ik/index.js +5 -0
  59. package/dist/ik/index.js.map +1 -0
  60. package/dist/index.js +27 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/noise/fractal.js +185 -0
  63. package/dist/noise/fractal.js.map +1 -0
  64. package/dist/noise/index.js +16 -0
  65. package/dist/noise/index.js.map +1 -0
  66. package/dist/noise/perlin2d.js +50 -0
  67. package/dist/noise/perlin2d.js.map +1 -0
  68. package/dist/noise/perlin3d.js +72 -0
  69. package/dist/noise/perlin3d.js.map +1 -0
  70. package/dist/noise/permutation.js +120 -0
  71. package/dist/noise/permutation.js.map +1 -0
  72. package/dist/noise/simplex2d.js +84 -0
  73. package/dist/noise/simplex2d.js.map +1 -0
  74. package/dist/noise/simplex3d.js +147 -0
  75. package/dist/noise/simplex3d.js.map +1 -0
  76. package/dist/noise/simplex4d.js +149 -0
  77. package/dist/noise/simplex4d.js.map +1 -0
  78. package/dist/noise/worley2d.js +50 -0
  79. package/dist/noise/worley2d.js.map +1 -0
  80. package/dist/noise/worley3d.js +61 -0
  81. package/dist/noise/worley3d.js.map +1 -0
  82. package/dist/random/index.js +9 -0
  83. package/dist/random/index.js.map +1 -0
  84. package/dist/random/isaac32.js +164 -0
  85. package/dist/random/isaac32.js.map +1 -0
  86. package/dist/random/isaac64.js +215 -0
  87. package/dist/random/isaac64.js.map +1 -0
  88. package/dist/random/mulberry32.js +48 -0
  89. package/dist/random/mulberry32.js.map +1 -0
  90. package/dist/random/random.js +120 -0
  91. package/dist/random/random.js.map +1 -0
  92. package/dist/shapes/box2.js +300 -0
  93. package/dist/shapes/box2.js.map +1 -0
  94. package/dist/shapes/box3.js +550 -0
  95. package/dist/shapes/box3.js.map +1 -0
  96. package/dist/shapes/circle.js +6 -0
  97. package/dist/shapes/circle.js.map +1 -0
  98. package/dist/shapes/frustum.js +662 -0
  99. package/dist/shapes/frustum.js.map +1 -0
  100. package/dist/shapes/index.js +25 -0
  101. package/dist/shapes/index.js.map +1 -0
  102. package/dist/shapes/obb3.js +557 -0
  103. package/dist/shapes/obb3.js.map +1 -0
  104. package/dist/shapes/plane3.js +267 -0
  105. package/dist/shapes/plane3.js.map +1 -0
  106. package/dist/shapes/polygon2.js +451 -0
  107. package/dist/shapes/polygon2.js.map +1 -0
  108. package/dist/shapes/raycast3.js +164 -0
  109. package/dist/shapes/raycast3.js.map +1 -0
  110. package/dist/shapes/segment2.js +82 -0
  111. package/dist/shapes/segment2.js.map +1 -0
  112. package/dist/shapes/sphere.js +23 -0
  113. package/dist/shapes/sphere.js.map +1 -0
  114. package/dist/shapes/triangle2.js +79 -0
  115. package/dist/shapes/triangle2.js.map +1 -0
  116. package/dist/shapes/triangle3.js +60 -0
  117. package/dist/shapes/triangle3.js.map +1 -0
  118. package/dist/src/color/color.d.ts +50 -0
  119. package/dist/src/color/colorspace.d.ts +15 -0
  120. package/dist/src/color/hsl.d.ts +28 -0
  121. package/dist/src/color/index.d.ts +5 -0
  122. package/dist/src/color/parse.d.ts +15 -0
  123. package/dist/src/core/angle.d.ts +29 -0
  124. package/dist/src/core/arrays.d.ts +4 -0
  125. package/dist/src/core/euler.d.ts +78 -0
  126. package/dist/src/core/index.d.ts +27 -0
  127. package/dist/src/core/mat2.d.ts +217 -0
  128. package/dist/src/core/mat2d.d.ts +218 -0
  129. package/dist/src/core/mat3.d.ts +295 -0
  130. package/dist/src/core/mat4.d.ts +667 -0
  131. package/dist/src/core/polar.d.ts +151 -0
  132. package/dist/src/core/quat.d.ts +370 -0
  133. package/dist/src/core/quat2.d.ts +344 -0
  134. package/dist/src/core/scalar.d.ts +75 -0
  135. package/dist/src/core/spherical.d.ts +165 -0
  136. package/dist/src/core/vec2.d.ts +403 -0
  137. package/dist/src/core/vec3.d.ts +493 -0
  138. package/dist/src/core/vec4.d.ts +336 -0
  139. package/dist/src/geometry/circumcircle.d.ts +9 -0
  140. package/dist/src/geometry/index.d.ts +5 -0
  141. package/dist/src/geometry/polygon2-decompose.d.ts +25 -0
  142. package/dist/src/geometry/polygon2-triangulate.d.ts +15 -0
  143. package/dist/src/geometry/quickhull2.d.ts +10 -0
  144. package/dist/src/geometry/quickhull3.d.ts +27 -0
  145. package/dist/src/ik/fabrik2.d.ts +314 -0
  146. package/dist/src/ik/fabrik3.d.ts +371 -0
  147. package/dist/src/ik/index.d.ts +2 -0
  148. package/dist/src/index.d.ts +1 -0
  149. package/dist/src/noise/fractal.d.ts +95 -0
  150. package/dist/src/noise/index.d.ts +9 -0
  151. package/dist/src/noise/perlin2d.d.ts +19 -0
  152. package/dist/src/noise/perlin3d.d.ts +20 -0
  153. package/dist/src/noise/permutation.d.ts +22 -0
  154. package/dist/src/noise/simplex2d.d.ts +19 -0
  155. package/dist/src/noise/simplex3d.d.ts +20 -0
  156. package/dist/src/noise/simplex4d.d.ts +24 -0
  157. package/dist/src/noise/worley2d.d.ts +22 -0
  158. package/dist/src/noise/worley3d.d.ts +23 -0
  159. package/dist/src/random/index.d.ts +8 -0
  160. package/dist/src/random/isaac32.d.ts +62 -0
  161. package/dist/src/random/isaac64.d.ts +75 -0
  162. package/dist/src/random/mulberry32.d.ts +43 -0
  163. package/dist/src/random/random.d.ts +67 -0
  164. package/dist/src/shapes/box2.d.ts +174 -0
  165. package/dist/src/shapes/box3.d.ts +213 -0
  166. package/dist/src/shapes/circle.d.ts +7 -0
  167. package/dist/src/shapes/frustum.d.ts +147 -0
  168. package/dist/src/shapes/index.d.ts +20 -0
  169. package/dist/src/shapes/obb3.d.ts +105 -0
  170. package/dist/src/shapes/plane3.d.ts +130 -0
  171. package/dist/src/shapes/polygon2.d.ts +155 -0
  172. package/dist/src/shapes/raycast3.d.ts +42 -0
  173. package/dist/src/shapes/segment2.d.ts +34 -0
  174. package/dist/src/shapes/sphere.d.ts +19 -0
  175. package/dist/src/shapes/triangle2.d.ts +53 -0
  176. package/dist/src/shapes/triangle3.d.ts +29 -0
  177. package/dist/src/time/easing.d.ts +21 -0
  178. package/dist/src/time/index.d.ts +6 -0
  179. package/dist/src/time/spring-core.d.ts +15 -0
  180. package/dist/src/time/spring.d.ts +36 -0
  181. package/dist/src/time/spring2.d.ts +11 -0
  182. package/dist/src/time/spring3.d.ts +11 -0
  183. package/dist/src/time/spring4.d.ts +11 -0
  184. package/dist/time/easing.js +24 -0
  185. package/dist/time/easing.js.map +1 -0
  186. package/dist/time/index.js +11 -0
  187. package/dist/time/index.js.map +1 -0
  188. package/dist/time/spring-core.js +48 -0
  189. package/dist/time/spring-core.js.map +1 -0
  190. package/dist/time/spring.js +62 -0
  191. package/dist/time/spring.js.map +1 -0
  192. package/dist/time/spring2.js +31 -0
  193. package/dist/time/spring2.js.map +1 -0
  194. package/dist/time/spring3.js +32 -0
  195. package/dist/time/spring3.js.map +1 -0
  196. package/dist/time/spring4.js +32 -0
  197. package/dist/time/spring4.js.map +1 -0
  198. package/package.json +90 -12
  199. package/skills/math/SKILL.md +140 -0
  200. package/math.coffee +0 -36
  201. package/math.js +0 -67
@@ -0,0 +1,61 @@
1
+ import { createPermutation } from './permutation.js';
2
+
3
+ /**
4
+ * Creates a 3D Worley noise generator with the given seed.
5
+ *
6
+ * @param seed The seed value for the noise generator
7
+ * @returns A generator to pass to {@link sample}
8
+ */
9
+ function create(seed) {
10
+ return createPermutation(seed);
11
+ }
12
+ /**
13
+ * Samples 3D Worley (cellular) noise: the Euclidean distance to the nearest of a
14
+ * set of feature points, one scattered per unit cell. Values are roughly in
15
+ * [0, 1] (0 right at a feature point). Threshold a 3D field for cave networks,
16
+ * ore pockets, or volumetric cell structures.
17
+ *
18
+ * @param generator A generator created with {@link create}
19
+ * @param x X coordinate
20
+ * @param y Y coordinate
21
+ * @param z Z coordinate
22
+ * @returns The distance to the nearest feature point (F1)
23
+ */
24
+ function sample({ perm }, x, y, z) {
25
+ const ix = Math.floor(x);
26
+ const iy = Math.floor(y);
27
+ const iz = Math.floor(z);
28
+ let f1 = Infinity; // squared nearest distance
29
+ // check the point's cell and its 26 neighbours. The hash is nested
30
+ // perm[x + perm[y + perm[z]]], so the z and y stages are shared by every
31
+ // cell in a slice / row and are computed once per slice / row.
32
+ for (let gz = -1; gz <= 1; gz++) {
33
+ const cz = iz + gz;
34
+ const pz = perm[cz & 255];
35
+ const qz = perm[(cz + 13) & 255];
36
+ const sz = perm[(cz + 97) & 255];
37
+ for (let gy = -1; gy <= 1; gy++) {
38
+ const cy = iy + gy;
39
+ const py = perm[(cy & 255) + pz];
40
+ const qy = perm[((cy + 37) & 255) + qz];
41
+ const sy = perm[((cy + 53) & 255) + sz];
42
+ for (let gx = -1; gx <= 1; gx++) {
43
+ const cx = ix + gx;
44
+ // a hashed feature-point offset in [0, 1) per axis, decorrelated
45
+ const rx = perm[(cx & 255) + py] / 256;
46
+ const ry = perm[((cx + 71) & 255) + qy] / 256;
47
+ const rz = perm[((cx + 23) & 255) + sy] / 256;
48
+ const dx = cx + rx - x;
49
+ const dy = cy + ry - y;
50
+ const dz = cz + rz - z;
51
+ const d2 = dx * dx + dy * dy + dz * dz;
52
+ if (d2 < f1)
53
+ f1 = d2;
54
+ }
55
+ }
56
+ }
57
+ return Math.sqrt(f1);
58
+ }
59
+
60
+ export { create, sample };
61
+ //# sourceMappingURL=worley3d.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worley3d.js","sources":["../../src/noise/worley3d.ts"],"sourcesContent":["import { createPermutation, type Permutation } from './permutation';\n\n/** A seeded 3D Worley (cellular) noise generator. Create one with {@link create}. */\nexport type Worley3DGenerator = Permutation;\n\n/**\n * Creates a 3D Worley noise generator with the given seed.\n *\n * @param seed The seed value for the noise generator\n * @returns A generator to pass to {@link sample}\n */\nexport function create(seed: number): Worley3DGenerator {\n return createPermutation(seed);\n}\n\n/**\n * Samples 3D Worley (cellular) noise: the Euclidean distance to the nearest of a\n * set of feature points, one scattered per unit cell. Values are roughly in\n * [0, 1] (0 right at a feature point). Threshold a 3D field for cave networks,\n * ore pockets, or volumetric cell structures.\n *\n * @param generator A generator created with {@link create}\n * @param x X coordinate\n * @param y Y coordinate\n * @param z Z coordinate\n * @returns The distance to the nearest feature point (F1)\n */\nexport function sample({ perm }: Worley3DGenerator, x: number, y: number, z: number): number {\n const ix = Math.floor(x);\n const iy = Math.floor(y);\n const iz = Math.floor(z);\n let f1 = Infinity; // squared nearest distance\n\n // check the point's cell and its 26 neighbours. The hash is nested\n // perm[x + perm[y + perm[z]]], so the z and y stages are shared by every\n // cell in a slice / row and are computed once per slice / row.\n for (let gz = -1; gz <= 1; gz++) {\n const cz = iz + gz;\n const pz = perm[cz & 255];\n const qz = perm[(cz + 13) & 255];\n const sz = perm[(cz + 97) & 255];\n for (let gy = -1; gy <= 1; gy++) {\n const cy = iy + gy;\n const py = perm[(cy & 255) + pz];\n const qy = perm[((cy + 37) & 255) + qz];\n const sy = perm[((cy + 53) & 255) + sz];\n for (let gx = -1; gx <= 1; gx++) {\n const cx = ix + gx;\n // a hashed feature-point offset in [0, 1) per axis, decorrelated\n const rx = perm[(cx & 255) + py] / 256;\n const ry = perm[((cx + 71) & 255) + qy] / 256;\n const rz = perm[((cx + 23) & 255) + sy] / 256;\n const dx = cx + rx - x;\n const dy = cy + ry - y;\n const dz = cz + rz - z;\n const d2 = dx * dx + dy * dy + dz * dz;\n if (d2 < f1) f1 = d2;\n }\n }\n }\n\n return Math.sqrt(f1);\n}\n"],"names":[],"mappings":";;AAK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C;AACvC,YAAA,KAAK,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,EAAE,EAAE;AAC7B,gBAAA,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE;;AAElB,gBAAA,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE,GAAG,GAAG,IAAI,EAAE,CAAC,GAAG,GAAG;AACtC,gBAAA,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,IAAI,EAAE,CAAC,GAAG,GAAG;AAC7C,gBAAA,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,IAAI,EAAE,CAAC,GAAG,GAAG;AAC7C,gBAAA,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;AACtB,gBAAA,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;AACtB,gBAAA,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;AACtB,gBAAA,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;gBACtC,IAAI,EAAE,GAAG,EAAE;oBAAE,EAAE,GAAG,EAAE;YACxB;QACJ;IACJ;AAEA,IAAA,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AACxB;;;;"}
@@ -0,0 +1,9 @@
1
+ import * as isaac32 from './isaac32.js';
2
+ export { isaac32 };
3
+ import * as isaac64 from './isaac64.js';
4
+ export { isaac64 };
5
+ import * as mulberry32 from './mulberry32.js';
6
+ export { mulberry32 };
7
+ import * as random from './random.js';
8
+ export { random };
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
@@ -0,0 +1,164 @@
1
+ // Bob Jenkins' seed mixing step (the golden-ratio scrambler from randinit).
2
+ function mix(s) {
3
+ s[0] = (s[0] ^ (s[1] << 11)) >>> 0;
4
+ s[3] = (s[3] + s[0]) >>> 0;
5
+ s[1] = (s[1] + s[2]) >>> 0;
6
+ s[1] = (s[1] ^ (s[2] >>> 2)) >>> 0;
7
+ s[4] = (s[4] + s[1]) >>> 0;
8
+ s[2] = (s[2] + s[3]) >>> 0;
9
+ s[2] = (s[2] ^ (s[3] << 8)) >>> 0;
10
+ s[5] = (s[5] + s[2]) >>> 0;
11
+ s[3] = (s[3] + s[4]) >>> 0;
12
+ s[3] = (s[3] ^ (s[4] >>> 16)) >>> 0;
13
+ s[6] = (s[6] + s[3]) >>> 0;
14
+ s[4] = (s[4] + s[5]) >>> 0;
15
+ s[4] = (s[4] ^ (s[5] << 10)) >>> 0;
16
+ s[7] = (s[7] + s[4]) >>> 0;
17
+ s[5] = (s[5] + s[6]) >>> 0;
18
+ s[5] = (s[5] ^ (s[6] >>> 4)) >>> 0;
19
+ s[0] = (s[0] + s[5]) >>> 0;
20
+ s[6] = (s[6] + s[7]) >>> 0;
21
+ s[6] = (s[6] ^ (s[7] << 8)) >>> 0;
22
+ s[1] = (s[1] + s[6]) >>> 0;
23
+ s[7] = (s[7] + s[0]) >>> 0;
24
+ s[7] = (s[7] ^ (s[0] >>> 9)) >>> 0;
25
+ s[2] = (s[2] + s[7]) >>> 0;
26
+ s[0] = (s[0] + s[1]) >>> 0;
27
+ }
28
+ /**
29
+ * Creates ISAAC-32 PRNG state seeded with `seed`.
30
+ *
31
+ * ISAAC is a fast, high-quality generator: its cycle length is at least 2^40 and
32
+ * ~2^8295 on average, and its output passes stringent statistical tests. Bob
33
+ * Jenkins designed it to resist prediction, but it is not founded on
34
+ * cryptographic theory, so **do not rely on it for cryptography** — use the Web
35
+ * Crypto API for that.
36
+ *
37
+ * A `seed` of 0 reproduces the reference implementation's unseeded output. For
38
+ * the full-strength 64-bit variant see {@link Isaac64}.
39
+ *
40
+ * @param seed the seed value (32-bit integer), defaults to 0
41
+ * @returns state to pass to {@link sample} or {@link next}
42
+ */
43
+ function create(seed = 0) {
44
+ const m = new Uint32Array(256);
45
+ m[0] = seed >>> 0;
46
+ const s = new Uint32Array(8).fill(0x9e3779b9); // the golden ratio
47
+ for (let k = 0; k < 4; k++)
48
+ mix(s); // scramble it
49
+ // fill m[], folding the seed in as we go
50
+ for (let i = 0; i < 256; i += 8) {
51
+ for (let k = 0; k < 8; k++)
52
+ s[k] = (s[k] + m[i + k]) >>> 0;
53
+ mix(s);
54
+ for (let k = 0; k < 8; k++)
55
+ m[i + k] = s[k];
56
+ }
57
+ return { m, r: new Uint32Array(256), a: 0, b: 0, c: 0, i: 256 };
58
+ }
59
+ // Generate the next batch of 256 results into state.r.
60
+ //
61
+ // This is Bob Jenkins' optimized rand.c form: two unrolled half-passes with the
62
+ // second operand (`m[j]`) walking its own index, which drops both the per-word
63
+ // `switch` on the shift pattern and the `(i + 128) % 256` wrap of the readable
64
+ // reference. `a`/`b` are hoisted into locals so the hot loop stays off the heap.
65
+ function refill(state) {
66
+ const m = state.m;
67
+ const r = state.r;
68
+ const c = (state.c + 1) >>> 0;
69
+ state.c = c;
70
+ let a = state.a;
71
+ let b = (state.b + c) >>> 0;
72
+ let i = 0;
73
+ let j = 128; // second operand starts a half-array ahead, then wraps to 0
74
+ let x = 0;
75
+ let y = 0;
76
+ // First half: i in [0, 128), j (the second operand) in [128, 256).
77
+ while (i < 128) {
78
+ x = m[i];
79
+ a = ((a ^ (a << 13)) + m[j++]) >>> 0;
80
+ y = (m[(x >>> 2) & 255] + a + b) >>> 0;
81
+ m[i] = y;
82
+ b = (m[(y >>> 10) & 255] + x) >>> 0;
83
+ r[i++] = b;
84
+ x = m[i];
85
+ a = ((a ^ (a >>> 6)) + m[j++]) >>> 0;
86
+ y = (m[(x >>> 2) & 255] + a + b) >>> 0;
87
+ m[i] = y;
88
+ b = (m[(y >>> 10) & 255] + x) >>> 0;
89
+ r[i++] = b;
90
+ x = m[i];
91
+ a = ((a ^ (a << 2)) + m[j++]) >>> 0;
92
+ y = (m[(x >>> 2) & 255] + a + b) >>> 0;
93
+ m[i] = y;
94
+ b = (m[(y >>> 10) & 255] + x) >>> 0;
95
+ r[i++] = b;
96
+ x = m[i];
97
+ a = ((a ^ (a >>> 16)) + m[j++]) >>> 0;
98
+ y = (m[(x >>> 2) & 255] + a + b) >>> 0;
99
+ m[i] = y;
100
+ b = (m[(y >>> 10) & 255] + x) >>> 0;
101
+ r[i++] = b;
102
+ }
103
+ // Second half: i in [128, 256), j wraps back to [0, 128).
104
+ j = 0;
105
+ while (i < 256) {
106
+ x = m[i];
107
+ a = ((a ^ (a << 13)) + m[j++]) >>> 0;
108
+ y = (m[(x >>> 2) & 255] + a + b) >>> 0;
109
+ m[i] = y;
110
+ b = (m[(y >>> 10) & 255] + x) >>> 0;
111
+ r[i++] = b;
112
+ x = m[i];
113
+ a = ((a ^ (a >>> 6)) + m[j++]) >>> 0;
114
+ y = (m[(x >>> 2) & 255] + a + b) >>> 0;
115
+ m[i] = y;
116
+ b = (m[(y >>> 10) & 255] + x) >>> 0;
117
+ r[i++] = b;
118
+ x = m[i];
119
+ a = ((a ^ (a << 2)) + m[j++]) >>> 0;
120
+ y = (m[(x >>> 2) & 255] + a + b) >>> 0;
121
+ m[i] = y;
122
+ b = (m[(y >>> 10) & 255] + x) >>> 0;
123
+ r[i++] = b;
124
+ x = m[i];
125
+ a = ((a ^ (a >>> 16)) + m[j++]) >>> 0;
126
+ y = (m[(x >>> 2) & 255] + a + b) >>> 0;
127
+ m[i] = y;
128
+ b = (m[(y >>> 10) & 255] + x) >>> 0;
129
+ r[i++] = b;
130
+ }
131
+ state.a = a;
132
+ state.b = b;
133
+ state.i = 0;
134
+ }
135
+ /**
136
+ * Advances `state` and returns the next raw 32-bit unsigned integer.
137
+ *
138
+ * @param state PRNG state created with {@link create}, mutated in place
139
+ * @returns an integer in the range [0, 2^32)
140
+ */
141
+ function next(state) {
142
+ if (state.i >= 256)
143
+ refill(state);
144
+ return state.r[state.i++];
145
+ }
146
+ /**
147
+ * Advances `state` and returns the next number in the range [0, 1).
148
+ *
149
+ * @param state PRNG state created with {@link create}, mutated in place
150
+ * @returns a number in the range [0, 1)
151
+ */
152
+ function sample(state) {
153
+ return next(state) / 4294967296;
154
+ }
155
+ /**
156
+ * Generates a random 32-bit unsigned integer seed, suitable for use with
157
+ * {@link create}.
158
+ */
159
+ function seed() {
160
+ return (Math.random() * 2 ** 32) >>> 0;
161
+ }
162
+
163
+ export { create, next, sample, seed };
164
+ //# sourceMappingURL=isaac32.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isaac32.js","sources":["../../src/random/isaac32.ts"],"sourcesContent":["/**\n * State of an ISAAC-32 PRNG: two 256-word arrays plus three accumulators and a\n * cursor into the current batch of results. Create one with {@link create}.\n *\n * ISAAC (Indirection, Shift, Accumulate, Add, Count) generates 256 words per\n * round and hands them out one at a time; {@link next}/{@link sample} refill the\n * batch automatically when it runs dry.\n *\n * The state is a plain object, so it can be inspected,\n * cloned (to fork a sequence), or serialised. `m`/`r` are typed arrays, so a\n * structural clone (e.g. `structuredClone`) forks correctly; a shallow `{...}`\n * copy shares them and does not.\n */\nexport type Isaac32 = {\n /** internal state (\"mem\"), 256 words */\n m: Uint32Array;\n /** current batch of results, 256 words */\n r: Uint32Array;\n /** accumulator */\n a: number;\n /** previous result */\n b: number;\n /** counter, incremented once per batch */\n c: number;\n /** cursor into `r`; a value of 256 means the batch is spent */\n i: number;\n};\n\n// Bob Jenkins' seed mixing step (the golden-ratio scrambler from randinit).\nfunction mix(s: Uint32Array): void {\n s[0] = (s[0] ^ (s[1] << 11)) >>> 0;\n s[3] = (s[3] + s[0]) >>> 0;\n s[1] = (s[1] + s[2]) >>> 0;\n s[1] = (s[1] ^ (s[2] >>> 2)) >>> 0;\n s[4] = (s[4] + s[1]) >>> 0;\n s[2] = (s[2] + s[3]) >>> 0;\n s[2] = (s[2] ^ (s[3] << 8)) >>> 0;\n s[5] = (s[5] + s[2]) >>> 0;\n s[3] = (s[3] + s[4]) >>> 0;\n s[3] = (s[3] ^ (s[4] >>> 16)) >>> 0;\n s[6] = (s[6] + s[3]) >>> 0;\n s[4] = (s[4] + s[5]) >>> 0;\n s[4] = (s[4] ^ (s[5] << 10)) >>> 0;\n s[7] = (s[7] + s[4]) >>> 0;\n s[5] = (s[5] + s[6]) >>> 0;\n s[5] = (s[5] ^ (s[6] >>> 4)) >>> 0;\n s[0] = (s[0] + s[5]) >>> 0;\n s[6] = (s[6] + s[7]) >>> 0;\n s[6] = (s[6] ^ (s[7] << 8)) >>> 0;\n s[1] = (s[1] + s[6]) >>> 0;\n s[7] = (s[7] + s[0]) >>> 0;\n s[7] = (s[7] ^ (s[0] >>> 9)) >>> 0;\n s[2] = (s[2] + s[7]) >>> 0;\n s[0] = (s[0] + s[1]) >>> 0;\n}\n\n/**\n * Creates ISAAC-32 PRNG state seeded with `seed`.\n *\n * ISAAC is a fast, high-quality generator: its cycle length is at least 2^40 and\n * ~2^8295 on average, and its output passes stringent statistical tests. Bob\n * Jenkins designed it to resist prediction, but it is not founded on\n * cryptographic theory, so **do not rely on it for cryptography** — use the Web\n * Crypto API for that.\n *\n * A `seed` of 0 reproduces the reference implementation's unseeded output. For\n * the full-strength 64-bit variant see {@link Isaac64}.\n *\n * @param seed the seed value (32-bit integer), defaults to 0\n * @returns state to pass to {@link sample} or {@link next}\n */\nexport function create(seed = 0): Isaac32 {\n const m = new Uint32Array(256);\n m[0] = seed >>> 0;\n\n const s = new Uint32Array(8).fill(0x9e3779b9); // the golden ratio\n for (let k = 0; k < 4; k++) mix(s); // scramble it\n\n // fill m[], folding the seed in as we go\n for (let i = 0; i < 256; i += 8) {\n for (let k = 0; k < 8; k++) s[k] = (s[k] + m[i + k]) >>> 0;\n mix(s);\n for (let k = 0; k < 8; k++) m[i + k] = s[k];\n }\n\n return { m, r: new Uint32Array(256), a: 0, b: 0, c: 0, i: 256 };\n}\n\n// Generate the next batch of 256 results into state.r.\n//\n// This is Bob Jenkins' optimized rand.c form: two unrolled half-passes with the\n// second operand (`m[j]`) walking its own index, which drops both the per-word\n// `switch` on the shift pattern and the `(i + 128) % 256` wrap of the readable\n// reference. `a`/`b` are hoisted into locals so the hot loop stays off the heap.\nfunction refill(state: Isaac32): void {\n const m = state.m;\n const r = state.r;\n const c = (state.c + 1) >>> 0;\n state.c = c;\n let a = state.a;\n let b = (state.b + c) >>> 0;\n\n let i = 0;\n let j = 128; // second operand starts a half-array ahead, then wraps to 0\n let x = 0;\n let y = 0;\n\n // First half: i in [0, 128), j (the second operand) in [128, 256).\n while (i < 128) {\n x = m[i];\n a = ((a ^ (a << 13)) + m[j++]) >>> 0;\n y = (m[(x >>> 2) & 255] + a + b) >>> 0;\n m[i] = y;\n b = (m[(y >>> 10) & 255] + x) >>> 0;\n r[i++] = b;\n\n x = m[i];\n a = ((a ^ (a >>> 6)) + m[j++]) >>> 0;\n y = (m[(x >>> 2) & 255] + a + b) >>> 0;\n m[i] = y;\n b = (m[(y >>> 10) & 255] + x) >>> 0;\n r[i++] = b;\n\n x = m[i];\n a = ((a ^ (a << 2)) + m[j++]) >>> 0;\n y = (m[(x >>> 2) & 255] + a + b) >>> 0;\n m[i] = y;\n b = (m[(y >>> 10) & 255] + x) >>> 0;\n r[i++] = b;\n\n x = m[i];\n a = ((a ^ (a >>> 16)) + m[j++]) >>> 0;\n y = (m[(x >>> 2) & 255] + a + b) >>> 0;\n m[i] = y;\n b = (m[(y >>> 10) & 255] + x) >>> 0;\n r[i++] = b;\n }\n\n // Second half: i in [128, 256), j wraps back to [0, 128).\n j = 0;\n while (i < 256) {\n x = m[i];\n a = ((a ^ (a << 13)) + m[j++]) >>> 0;\n y = (m[(x >>> 2) & 255] + a + b) >>> 0;\n m[i] = y;\n b = (m[(y >>> 10) & 255] + x) >>> 0;\n r[i++] = b;\n\n x = m[i];\n a = ((a ^ (a >>> 6)) + m[j++]) >>> 0;\n y = (m[(x >>> 2) & 255] + a + b) >>> 0;\n m[i] = y;\n b = (m[(y >>> 10) & 255] + x) >>> 0;\n r[i++] = b;\n\n x = m[i];\n a = ((a ^ (a << 2)) + m[j++]) >>> 0;\n y = (m[(x >>> 2) & 255] + a + b) >>> 0;\n m[i] = y;\n b = (m[(y >>> 10) & 255] + x) >>> 0;\n r[i++] = b;\n\n x = m[i];\n a = ((a ^ (a >>> 16)) + m[j++]) >>> 0;\n y = (m[(x >>> 2) & 255] + a + b) >>> 0;\n m[i] = y;\n b = (m[(y >>> 10) & 255] + x) >>> 0;\n r[i++] = b;\n }\n\n state.a = a;\n state.b = b;\n state.i = 0;\n}\n\n/**\n * Advances `state` and returns the next raw 32-bit unsigned integer.\n *\n * @param state PRNG state created with {@link create}, mutated in place\n * @returns an integer in the range [0, 2^32)\n */\nexport function next(state: Isaac32): number {\n if (state.i >= 256) refill(state);\n return state.r[state.i++];\n}\n\n/**\n * Advances `state` and returns the next number in the range [0, 1).\n *\n * @param state PRNG state created with {@link create}, mutated in place\n * @returns a number in the range [0, 1)\n */\nexport function sample(state: Isaac32): number {\n return next(state) / 4294967296;\n}\n\n/**\n * Generates a random 32-bit unsigned integer seed, suitable for use with\n * {@link create}.\n */\nexport function seed(): number {\n return (Math.random() * 2 ** 32) >>> 0;\n}\n"],"names":[],"mappings":"AA4BA;AACA,SAAS,GAAG,CAAC,CAAc,EAAA;IACvB,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC;AAClC,IAAA,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;AAC1B,IAAA,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAC1B,CAAC,CAAC,CAAC,CAAC,GA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,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC;AAC1C;;;;"}
@@ -0,0 +1,215 @@
1
+ const MASK = 0xffffffffffffffffn;
2
+ // The golden-ratio constant 0x9e3779b97f4a7c15 pre-scrambled by the reference's
3
+ // four mixing rounds — the seed-mixing starting accumulators.
4
+ const SCRAMBLED = [
5
+ 0x647c4677a2884b7cn,
6
+ 0xb9f8b322c73ac862n,
7
+ 0x8c0ea5053d4712a0n,
8
+ 0xb29b2e824a595524n,
9
+ 0x82f053db8355e0cen,
10
+ 0x48fe4a0fa5a09315n,
11
+ 0xae985bf2cbfc89edn,
12
+ 0x98f5704f6c44c0abn,
13
+ ];
14
+ /**
15
+ * Creates ISAAC64 PRNG state seeded with `seed`.
16
+ *
17
+ * ISAAC64 is a fast, high-quality generator with an enormous cycle length
18
+ * (~2^8295 on average). Bob Jenkins designed it to resist prediction, but it is
19
+ * not founded on cryptographic theory, so **do not rely on it for
20
+ * cryptography** — use the Web Crypto API for that.
21
+ *
22
+ * A `seed` of 0 reproduces the reference implementation's unseeded output.
23
+ *
24
+ * @param seed the seed value (64-bit integer), defaults to 0n
25
+ * @returns state to pass to {@link sample} or {@link next}
26
+ */
27
+ function create(seed = 0n) {
28
+ // Seed mixing is one-time (not hot), so it stays in 64-bit `bigint`; the
29
+ // resulting `m` is then split into the 32-bit lanes the refill loop uses.
30
+ const m = new BigUint64Array(256);
31
+ m[0] = seed & MASK;
32
+ const a = SCRAMBLED.slice();
33
+ for (let j = 0; j < 256; j += 8) {
34
+ for (let x = 0; x < 8; x++)
35
+ a[x] = (a[x] + m[j + x]) & MASK;
36
+ a[0] = (a[0] - a[4]) & MASK;
37
+ a[5] ^= a[7] >> 9n;
38
+ a[7] = (a[7] + a[0]) & MASK;
39
+ a[1] = (a[1] - a[5]) & MASK;
40
+ a[6] ^= (a[0] << 9n) & MASK;
41
+ a[0] = (a[0] + a[1]) & MASK;
42
+ a[2] = (a[2] - a[6]) & MASK;
43
+ a[7] ^= a[1] >> 23n;
44
+ a[1] = (a[1] + a[2]) & MASK;
45
+ a[3] = (a[3] - a[7]) & MASK;
46
+ a[0] ^= (a[2] << 15n) & MASK;
47
+ a[2] = (a[2] + a[3]) & MASK;
48
+ a[4] = (a[4] - a[0]) & MASK;
49
+ a[1] ^= a[3] >> 14n;
50
+ a[3] = (a[3] + a[4]) & MASK;
51
+ a[5] = (a[5] - a[1]) & MASK;
52
+ a[2] ^= (a[4] << 20n) & MASK;
53
+ a[4] = (a[4] + a[5]) & MASK;
54
+ a[6] = (a[6] - a[2]) & MASK;
55
+ a[3] ^= a[5] >> 17n;
56
+ a[5] = (a[5] + a[6]) & MASK;
57
+ a[7] = (a[7] - a[3]) & MASK;
58
+ a[4] ^= (a[6] << 14n) & MASK;
59
+ a[6] = (a[6] + a[7]) & MASK;
60
+ for (let x = 0; x < 8; x++)
61
+ m[j + x] = a[x];
62
+ }
63
+ const mHi = new Uint32Array(256);
64
+ const mLo = new Uint32Array(256);
65
+ for (let k = 0; k < 256; k++) {
66
+ const v = m[k];
67
+ mLo[k] = Number(v & 0xffffffffn);
68
+ mHi[k] = Number(v >> 32n);
69
+ }
70
+ return {
71
+ mHi,
72
+ mLo,
73
+ rHi: new Uint32Array(256),
74
+ rLo: new Uint32Array(256),
75
+ aHi: 0,
76
+ aLo: 0,
77
+ bHi: 0,
78
+ bLo: 0,
79
+ cHi: 0,
80
+ cLo: 0,
81
+ i: 256,
82
+ };
83
+ }
84
+ const TWO32 = 4294967296; // 2^32, the carry threshold
85
+ // Generate the next batch of 256 results into state.r{Hi,Lo}.
86
+ //
87
+ // Bob Jenkins' ISAAC64 on split 32-bit lanes: each 64-bit add is a lane add plus
88
+ // a carry into the high lane, and the 64-bit shifts (21/5/12/33) are done by
89
+ // hand across the lane boundary. Results are written in reverse (r[255 - i]) to
90
+ // match the reference stream order. `a`/`b` are hoisted into locals so the loop
91
+ // stays in fast Word32 integer math.
92
+ function refill(state) {
93
+ const mHi = state.mHi;
94
+ const mLo = state.mLo;
95
+ const rHi = state.rHi;
96
+ const rLo = state.rLo;
97
+ // c += 1
98
+ const cLo = (state.cLo + 1) >>> 0;
99
+ let cHi = state.cHi;
100
+ if (cLo === 0)
101
+ cHi = (cHi + 1) >>> 0;
102
+ state.cLo = cLo;
103
+ state.cHi = cHi;
104
+ // b += c
105
+ const b0 = state.bLo + cLo;
106
+ let bLo = b0 >>> 0;
107
+ let bHi = (state.bHi + cHi + (b0 >= TWO32 ? 1 : 0)) >>> 0;
108
+ let aHi = state.aHi;
109
+ let aLo = state.aLo;
110
+ for (let i = 0; i < 256; i++) {
111
+ // mixed = (~)(a ^ (a << k)) / (a ^ (a >> k)), the shift pattern cycling
112
+ // every four words. Only the mix differs between the four cases.
113
+ let mxHi;
114
+ let mxLo;
115
+ let tHi;
116
+ let tLo;
117
+ switch (i & 3) {
118
+ case 0: // ~(a ^ (a << 21))
119
+ tHi = ((aHi << 21) | (aLo >>> 11)) >>> 0;
120
+ tLo = (aLo << 21) >>> 0;
121
+ mxHi = ~(aHi ^ tHi) >>> 0;
122
+ mxLo = ~(aLo ^ tLo) >>> 0;
123
+ break;
124
+ case 1: // a ^ (a >> 5)
125
+ tLo = ((aLo >>> 5) | (aHi << 27)) >>> 0;
126
+ tHi = aHi >>> 5;
127
+ mxHi = (aHi ^ tHi) >>> 0;
128
+ mxLo = (aLo ^ tLo) >>> 0;
129
+ break;
130
+ case 2: // a ^ (a << 12)
131
+ tHi = ((aHi << 12) | (aLo >>> 20)) >>> 0;
132
+ tLo = (aLo << 12) >>> 0;
133
+ mxHi = (aHi ^ tHi) >>> 0;
134
+ mxLo = (aLo ^ tLo) >>> 0;
135
+ break;
136
+ default: // a ^ (a >> 33) — shifts entirely into the low lane
137
+ mxLo = (aLo ^ (aHi >>> 1)) >>> 0;
138
+ mxHi = aHi;
139
+ }
140
+ const i2 = (i + 128) & 255; // the second operand, half an array ahead
141
+ const xLo = mLo[i];
142
+ const xHi = mHi[i];
143
+ // a = mixed + m[i2]
144
+ const s0 = mxLo + mLo[i2];
145
+ aLo = s0 >>> 0;
146
+ aHi = (mxHi + mHi[i2] + (s0 >= TWO32 ? 1 : 0)) >>> 0;
147
+ // y = a + b + m[(x >> 3) & 255] (the low 8 index bits live in the low lane)
148
+ const idx = (xLo >>> 3) & 255;
149
+ let abLo = aLo + bLo;
150
+ const abHi = (aHi + bHi + (abLo >= TWO32 ? 1 : 0)) >>> 0;
151
+ abLo = abLo >>> 0;
152
+ let yLo = abLo + mLo[idx];
153
+ const yHi = (abHi + mHi[idx] + (yLo >= TWO32 ? 1 : 0)) >>> 0;
154
+ yLo = yLo >>> 0;
155
+ mLo[i] = yLo;
156
+ mHi[i] = yHi;
157
+ // b = x + m[(y >> 11) & 255]
158
+ const idx2 = (yLo >>> 11) & 255;
159
+ const t = xLo + mLo[idx2];
160
+ bHi = (xHi + mHi[idx2] + (t >= TWO32 ? 1 : 0)) >>> 0;
161
+ bLo = t >>> 0;
162
+ const o = 255 - i;
163
+ rLo[o] = bLo;
164
+ rHi[o] = bHi;
165
+ }
166
+ state.aHi = aHi;
167
+ state.aLo = aLo;
168
+ state.bHi = bHi;
169
+ state.bLo = bLo;
170
+ state.i = 0;
171
+ }
172
+ /**
173
+ * Advances `state` and returns the next raw 64-bit unsigned integer.
174
+ *
175
+ * This reassembles a `bigint` from the two lanes, so it allocates; for a value
176
+ * in [0, 1) prefer {@link sample}, which stays in `number` arithmetic.
177
+ *
178
+ * @param state PRNG state created with {@link create}, mutated in place
179
+ * @returns an integer in the range [0, 2^64)
180
+ */
181
+ function next(state) {
182
+ if (state.i >= 256)
183
+ refill(state);
184
+ const i = state.i++;
185
+ return (BigInt(state.rHi[i]) << 32n) | BigInt(state.rLo[i]);
186
+ }
187
+ /**
188
+ * Advances `state` and returns the next number in the range [0, 1).
189
+ *
190
+ * The result carries 53 bits of randomness (the width of a double's mantissa),
191
+ * taken from the high bits of a 64-bit word — read straight from the lanes, so
192
+ * no `bigint` is allocated.
193
+ *
194
+ * @param state PRNG state created with {@link create}, mutated in place
195
+ * @returns a number in the range [0, 1)
196
+ */
197
+ function sample(state) {
198
+ if (state.i >= 256)
199
+ refill(state);
200
+ const i = state.i++;
201
+ // top 53 bits = (word >> 11) = hi * 2^21 + (lo >>> 11), exact in a double
202
+ return (state.rHi[i] * 2097152 + (state.rLo[i] >>> 11)) / 9007199254740992; // 2^21, 2^53
203
+ }
204
+ /**
205
+ * Generates a random 64-bit unsigned integer seed, suitable for use with
206
+ * {@link create}.
207
+ */
208
+ function seed() {
209
+ const hi = BigInt((Math.random() * 2 ** 32) >>> 0);
210
+ const lo = BigInt((Math.random() * 2 ** 32) >>> 0);
211
+ return (hi << 32n) | lo;
212
+ }
213
+
214
+ export { create, next, sample, seed };
215
+ //# sourceMappingURL=isaac64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isaac64.js","sources":["../../src/random/isaac64.ts"],"sourcesContent":["/**\n * State of an ISAAC64 PRNG. Create one with {@link create}.\n *\n * ISAAC64 is the 64-bit variant of ISAAC (Indirection, Shift, Accumulate, Add,\n * Count): same design as {@link Isaac32} but on 64-bit words, giving a larger\n * state and 64-bit output. JavaScript has no native 64-bit integers, so each\n * 64-bit word is split into two 32-bit lanes — `*Hi` (bits 63..32) and `*Lo`\n * (bits 31..0) — and the hot loop does 64-bit math with explicit carries in\n * plain `number`s. That keeps the whole generator in fast Word32 arithmetic\n * (see the note on {@link Isaac32}'s typed arrays); a `bigint` implementation is\n * ~6x slower. `next` reassembles a `bigint`; `sample` skips `bigint` entirely.\n *\n * The state is a plain object, so it can be inspected, cloned (to fork a\n * sequence), or serialised. The lanes are typed arrays, so a structural clone\n * (e.g. `structuredClone`) forks correctly; a shallow `{...}` copy shares them.\n */\nexport type Isaac64 = {\n /** internal state (\"mem\") high/low lanes, 256 words */\n mHi: Uint32Array;\n mLo: Uint32Array;\n /** current batch of results, high/low lanes, 256 words */\n rHi: Uint32Array;\n rLo: Uint32Array;\n /** accumulator */\n aHi: number;\n aLo: number;\n /** previous result */\n bHi: number;\n bLo: number;\n /** counter, incremented once per batch */\n cHi: number;\n cLo: number;\n /** cursor into `r`; a value of 256 means the batch is spent */\n i: number;\n};\n\nconst MASK = 0xffffffffffffffffn;\n\n// The golden-ratio constant 0x9e3779b97f4a7c15 pre-scrambled by the reference's\n// four mixing rounds — the seed-mixing starting accumulators.\nconst SCRAMBLED = [\n 0x647c4677a2884b7cn,\n 0xb9f8b322c73ac862n,\n 0x8c0ea5053d4712a0n,\n 0xb29b2e824a595524n,\n 0x82f053db8355e0cen,\n 0x48fe4a0fa5a09315n,\n 0xae985bf2cbfc89edn,\n 0x98f5704f6c44c0abn,\n];\n\n/**\n * Creates ISAAC64 PRNG state seeded with `seed`.\n *\n * ISAAC64 is a fast, high-quality generator with an enormous cycle length\n * (~2^8295 on average). Bob Jenkins designed it to resist prediction, but it is\n * not founded on cryptographic theory, so **do not rely on it for\n * cryptography** — use the Web Crypto API for that.\n *\n * A `seed` of 0 reproduces the reference implementation's unseeded output.\n *\n * @param seed the seed value (64-bit integer), defaults to 0n\n * @returns state to pass to {@link sample} or {@link next}\n */\nexport function create(seed: bigint = 0n): Isaac64 {\n // Seed mixing is one-time (not hot), so it stays in 64-bit `bigint`; the\n // resulting `m` is then split into the 32-bit lanes the refill loop uses.\n const m = new BigUint64Array(256);\n m[0] = seed & MASK;\n\n const a = SCRAMBLED.slice();\n for (let j = 0; j < 256; j += 8) {\n for (let x = 0; x < 8; x++) a[x] = (a[x] + m[j + x]) & MASK;\n a[0] = (a[0] - a[4]) & MASK;\n a[5] ^= a[7] >> 9n;\n a[7] = (a[7] + a[0]) & MASK;\n a[1] = (a[1] - a[5]) & MASK;\n a[6] ^= (a[0] << 9n) & MASK;\n a[0] = (a[0] + a[1]) & MASK;\n a[2] = (a[2] - a[6]) & MASK;\n a[7] ^= a[1] >> 23n;\n a[1] = (a[1] + a[2]) & MASK;\n a[3] = (a[3] - a[7]) & MASK;\n a[0] ^= (a[2] << 15n) & MASK;\n a[2] = (a[2] + a[3]) & MASK;\n a[4] = (a[4] - a[0]) & MASK;\n a[1] ^= a[3] >> 14n;\n a[3] = (a[3] + a[4]) & MASK;\n a[5] = (a[5] - a[1]) & MASK;\n a[2] ^= (a[4] << 20n) & MASK;\n a[4] = (a[4] + a[5]) & MASK;\n a[6] = (a[6] - a[2]) & MASK;\n a[3] ^= a[5] >> 17n;\n a[5] = (a[5] + a[6]) & MASK;\n a[7] = (a[7] - a[3]) & MASK;\n a[4] ^= (a[6] << 14n) & MASK;\n a[6] = (a[6] + a[7]) & MASK;\n for (let x = 0; x < 8; x++) m[j + x] = a[x];\n }\n\n const mHi = new Uint32Array(256);\n const mLo = new Uint32Array(256);\n for (let k = 0; k < 256; k++) {\n const v = m[k];\n mLo[k] = Number(v & 0xffffffffn);\n mHi[k] = Number(v >> 32n);\n }\n\n return {\n mHi,\n mLo,\n rHi: new Uint32Array(256),\n rLo: new Uint32Array(256),\n aHi: 0,\n aLo: 0,\n bHi: 0,\n bLo: 0,\n cHi: 0,\n cLo: 0,\n i: 256,\n };\n}\n\nconst TWO32 = 4294967296; // 2^32, the carry threshold\n\n// Generate the next batch of 256 results into state.r{Hi,Lo}.\n//\n// Bob Jenkins' ISAAC64 on split 32-bit lanes: each 64-bit add is a lane add plus\n// a carry into the high lane, and the 64-bit shifts (21/5/12/33) are done by\n// hand across the lane boundary. Results are written in reverse (r[255 - i]) to\n// match the reference stream order. `a`/`b` are hoisted into locals so the loop\n// stays in fast Word32 integer math.\nfunction refill(state: Isaac64): void {\n const mHi = state.mHi;\n const mLo = state.mLo;\n const rHi = state.rHi;\n const rLo = state.rLo;\n\n // c += 1\n const cLo = (state.cLo + 1) >>> 0;\n let cHi = state.cHi;\n if (cLo === 0) cHi = (cHi + 1) >>> 0;\n state.cLo = cLo;\n state.cHi = cHi;\n\n // b += c\n const b0 = state.bLo + cLo;\n let bLo = b0 >>> 0;\n let bHi = (state.bHi + cHi + (b0 >= TWO32 ? 1 : 0)) >>> 0;\n\n let aHi = state.aHi;\n let aLo = state.aLo;\n\n for (let i = 0; i < 256; i++) {\n // mixed = (~)(a ^ (a << k)) / (a ^ (a >> k)), the shift pattern cycling\n // every four words. Only the mix differs between the four cases.\n let mxHi: number;\n let mxLo: number;\n let tHi: number;\n let tLo: number;\n switch (i & 3) {\n case 0: // ~(a ^ (a << 21))\n tHi = ((aHi << 21) | (aLo >>> 11)) >>> 0;\n tLo = (aLo << 21) >>> 0;\n mxHi = ~(aHi ^ tHi) >>> 0;\n mxLo = ~(aLo ^ tLo) >>> 0;\n break;\n case 1: // a ^ (a >> 5)\n tLo = ((aLo >>> 5) | (aHi << 27)) >>> 0;\n tHi = aHi >>> 5;\n mxHi = (aHi ^ tHi) >>> 0;\n mxLo = (aLo ^ tLo) >>> 0;\n break;\n case 2: // a ^ (a << 12)\n tHi = ((aHi << 12) | (aLo >>> 20)) >>> 0;\n tLo = (aLo << 12) >>> 0;\n mxHi = (aHi ^ tHi) >>> 0;\n mxLo = (aLo ^ tLo) >>> 0;\n break;\n default: // a ^ (a >> 33) — shifts entirely into the low lane\n mxLo = (aLo ^ (aHi >>> 1)) >>> 0;\n mxHi = aHi;\n }\n\n const i2 = (i + 128) & 255; // the second operand, half an array ahead\n const xLo = mLo[i];\n const xHi = mHi[i];\n\n // a = mixed + m[i2]\n const s0 = mxLo + mLo[i2];\n aLo = s0 >>> 0;\n aHi = (mxHi + mHi[i2] + (s0 >= TWO32 ? 1 : 0)) >>> 0;\n\n // y = a + b + m[(x >> 3) & 255] (the low 8 index bits live in the low lane)\n const idx = (xLo >>> 3) & 255;\n let abLo = aLo + bLo;\n const abHi = (aHi + bHi + (abLo >= TWO32 ? 1 : 0)) >>> 0;\n abLo = abLo >>> 0;\n let yLo = abLo + mLo[idx];\n const yHi = (abHi + mHi[idx] + (yLo >= TWO32 ? 1 : 0)) >>> 0;\n yLo = yLo >>> 0;\n mLo[i] = yLo;\n mHi[i] = yHi;\n\n // b = x + m[(y >> 11) & 255]\n const idx2 = (yLo >>> 11) & 255;\n const t = xLo + mLo[idx2];\n bHi = (xHi + mHi[idx2] + (t >= TWO32 ? 1 : 0)) >>> 0;\n bLo = t >>> 0;\n\n const o = 255 - i;\n rLo[o] = bLo;\n rHi[o] = bHi;\n }\n\n state.aHi = aHi;\n state.aLo = aLo;\n state.bHi = bHi;\n state.bLo = bLo;\n state.i = 0;\n}\n\n/**\n * Advances `state` and returns the next raw 64-bit unsigned integer.\n *\n * This reassembles a `bigint` from the two lanes, so it allocates; for a value\n * in [0, 1) prefer {@link sample}, which stays in `number` arithmetic.\n *\n * @param state PRNG state created with {@link create}, mutated in place\n * @returns an integer in the range [0, 2^64)\n */\nexport function next(state: Isaac64): bigint {\n if (state.i >= 256) refill(state);\n const i = state.i++;\n return (BigInt(state.rHi[i]) << 32n) | BigInt(state.rLo[i]);\n}\n\n/**\n * Advances `state` and returns the next number in the range [0, 1).\n *\n * The result carries 53 bits of randomness (the width of a double's mantissa),\n * taken from the high bits of a 64-bit word — read straight from the lanes, so\n * no `bigint` is allocated.\n *\n * @param state PRNG state created with {@link create}, mutated in place\n * @returns a number in the range [0, 1)\n */\nexport function sample(state: Isaac64): number {\n if (state.i >= 256) refill(state);\n const i = state.i++;\n // top 53 bits = (word >> 11) = hi * 2^21 + (lo >>> 11), exact in a double\n return (state.rHi[i] * 2097152 + (state.rLo[i] >>> 11)) / 9007199254740992; // 2^21, 2^53\n}\n\n/**\n * Generates a random 64-bit unsigned integer seed, suitable for use with\n * {@link create}.\n */\nexport function seed(): bigint {\n const hi = BigInt((Math.random() * 2 ** 32) >>> 0);\n const lo = BigInt((Math.random() * 2 ** 32) >>> 0);\n return (hi << 32n) | lo;\n}\n"],"names":[],"mappings":"AAoCA,MAAM,IAAI,GAAG,mBAAmB;AAEhC;AACA;AACA,MAAM,SAAS,GAAG;IACd,mBAAmB;IACnB,mBAAmB;IACnB,mBAAmB;IACnB,mBAAmB;IACnB,mBAAmB;IACnB,mBAAmB;I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AAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAClD,IAAA,MAAM,EAAE,GAAG,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAClD,IAAA,OAAO,CAAC,EAAE,IAAI,GAAG,IAAI,EAAE;AAC3B;;;;"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Creates Mulberry32 PRNG state seeded with `seed`.
3
+ *
4
+ * Mulberry32 is a simple, fast, and effective PRNG that passes statistical tests
5
+ * and has good distribution properties.
6
+ *
7
+ * @param seed the seed value (32-bit integer)
8
+ * @returns state to pass to {@link sample}
9
+ */
10
+ function create(seed) {
11
+ return { a: seed };
12
+ }
13
+ /**
14
+ * Advances `state` and returns the next raw 32-bit unsigned integer.
15
+ *
16
+ * The accumulator is kept to 32 bits with `| 0`; without it, `state.a` would grow
17
+ * as an unbounded float and lose integer precision past 2^53 (~5M draws),
18
+ * corrupting the sequence.
19
+ *
20
+ * @param state PRNG state created with {@link create}, mutated in place
21
+ * @returns an integer in the range [0, 2^32)
22
+ */
23
+ function next(state) {
24
+ state.a = (state.a + 0x6d2b79f5) | 0;
25
+ let t = state.a;
26
+ t = Math.imul(t ^ (t >>> 15), t | 1);
27
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
28
+ return (t ^ (t >>> 14)) >>> 0;
29
+ }
30
+ /**
31
+ * Advances `state` and returns the next number in the range [0, 1).
32
+ *
33
+ * @param state PRNG state created with {@link create}, mutated in place
34
+ * @returns a number in the range [0, 1)
35
+ */
36
+ function sample(state) {
37
+ return next(state) / 4294967296;
38
+ }
39
+ /**
40
+ * Generates a random 32-bit unsigned integer seed, suitable for use with
41
+ * {@link create}.
42
+ */
43
+ function seed() {
44
+ return (Math.random() * 2 ** 32) >>> 0;
45
+ }
46
+
47
+ export { create, next, sample, seed };
48
+ //# sourceMappingURL=mulberry32.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mulberry32.js","sources":["../../src/random/mulberry32.ts"],"sourcesContent":["/**\n * State of a Mulberry32 PRNG: a single 32-bit accumulator that {@link sample}\n * advances on each call. Create one with {@link create}.\n *\n * Unlike a closure-based generator, the state is a plain object — so it can be\n * inspected, cloned (to fork a sequence), or serialised.\n */\nexport type Mulberry32 = { a: number };\n\n/**\n * Creates Mulberry32 PRNG state seeded with `seed`.\n *\n * Mulberry32 is a simple, fast, and effective PRNG that passes statistical tests\n * and has good distribution properties.\n *\n * @param seed the seed value (32-bit integer)\n * @returns state to pass to {@link sample}\n */\nexport function create(seed: number): Mulberry32 {\n return { a: seed };\n}\n\n/**\n * Advances `state` and returns the next raw 32-bit unsigned integer.\n *\n * The accumulator is kept to 32 bits with `| 0`; without it, `state.a` would grow\n * as an unbounded float and lose integer precision past 2^53 (~5M draws),\n * corrupting the sequence.\n *\n * @param state PRNG state created with {@link create}, mutated in place\n * @returns an integer in the range [0, 2^32)\n */\nexport function next(state: Mulberry32): number {\n state.a = (state.a + 0x6d2b79f5) | 0;\n let t = state.a;\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n return (t ^ (t >>> 14)) >>> 0;\n}\n\n/**\n * Advances `state` and returns the next number in the range [0, 1).\n *\n * @param state PRNG state created with {@link create}, mutated in place\n * @returns a number in the range [0, 1)\n */\nexport function sample(state: Mulberry32): number {\n return next(state) / 4294967296;\n}\n\n/**\n * Generates a random 32-bit unsigned integer seed, suitable for use with\n * {@link create}.\n */\nexport function seed(): number {\n return (Math.random() * 2 ** 32) >>> 0;\n}\n"],"names":[],"mappings":"AASA;;;;;;;;AAQG;AACG,SAAU,MAAM,CAAC,IAAY,EAAA;AAC/B,IAAA,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE;AACtB;AAEA;;;;;;;;;AASG;AACG,SAAU,IAAI,CAAC,KAAiB,EAAA;AAClC,IAAA,KAAK,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,UAAU,IAAI,CAAC;AACpC,IAAA,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC;AACf,IAAA,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;AACpC,IAAA,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC;IACzC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,MAAM,CAAC;AACjC;AAEA;;;;;AAKG;AACG,SAAU,MAAM,CAAC,KAAiB,EAAA;AACpC,IAAA,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,UAAU;AACnC;AAEA;;;AAGG;SACa,IAAI,GAAA;AAChB,IAAA,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC;AAC1C;;;;"}