Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. using NUnit.Framework;
  2. using System;
  3. using static Unity.Mathematics.math;
  4. using Burst.Compiler.IL.Tests;
  5. namespace Unity.Mathematics.Tests
  6. {
  7. [TestFixture]
  8. class TestRandom
  9. {
  10. // Kolmogorov–Smirnov test on lambda assuming the ideal distribution is uniform [0, 1]
  11. private static void ks_test(Func<double> func, int num_buckets = 256)
  12. {
  13. const int N = 8192;
  14. var histogram = new int[num_buckets];
  15. for (int i = 0; i < N; i++)
  16. {
  17. double x = func();
  18. Assert.GreaterOrEqual(x, 0.0);
  19. Assert.LessOrEqual(x, 1.0);
  20. int bucket = min((int)(x * num_buckets), num_buckets - 1);
  21. histogram[bucket]++;
  22. }
  23. double largest_delta = 0.0f;
  24. int accum = 0;
  25. for (int i = 0; i < histogram.Length; i++)
  26. {
  27. accum += histogram[i];
  28. double current = accum / (double)N;
  29. double target = (double)(i + 1) / histogram.Length;
  30. largest_delta = math.max(largest_delta, math.abs(current - target));
  31. }
  32. double d = 1.62762 / math.sqrt((double)N); // significance: 0.01
  33. Assert.Less(largest_delta, d);
  34. }
  35. private static void ks_test(Func<double2> func)
  36. {
  37. ks_test(() => func().x);
  38. ks_test(() => func().y);
  39. }
  40. // Pearson's product-moment coefficient
  41. private static void r_test(Func<double2> func)
  42. {
  43. const int N = 4096;
  44. double2 sum = 0.0;
  45. var values = new double2[N];
  46. for(int i = 0; i < N; i++)
  47. {
  48. values[i] = func();
  49. sum += values[i];
  50. }
  51. double2 avg = sum / N;
  52. double var_a = 0.0;
  53. double var_b = 0.0;
  54. double cov = 0.0;
  55. for (int i = 0; i < N; i++)
  56. {
  57. double2 delta = values[i] - avg;
  58. var_a += delta.x * delta.x;
  59. var_b += delta.y * delta.y;
  60. cov += delta.x * delta.y;
  61. }
  62. double r = cov / sqrt(var_a * var_b);
  63. Assert.Less(abs(r), 0.05);
  64. }
  65. private static float range_check01(float x)
  66. {
  67. Assert.GreaterOrEqual(x, 0.0f);
  68. Assert.Less(x, 1.0f);
  69. return x;
  70. }
  71. private static double range_check01(double x)
  72. {
  73. Assert.GreaterOrEqual(x, 0.0);
  74. Assert.Less(x, 1.0);
  75. return x;
  76. }
  77. private static int range_check(int x, int min, int max)
  78. {
  79. Assert.GreaterOrEqual(x, min);
  80. Assert.Less(x, max);
  81. return x;
  82. }
  83. private static uint range_check(uint x, uint min, uint max)
  84. {
  85. Assert.GreaterOrEqual(x, min);
  86. Assert.Less(x, max);
  87. return x;
  88. }
  89. private static float range_check(float x, float min, float max)
  90. {
  91. Assert.GreaterOrEqual(x, min);
  92. Assert.Less(x, max);
  93. return x;
  94. }
  95. private static double range_check(double x, double min, double max)
  96. {
  97. Assert.GreaterOrEqual(x, min);
  98. Assert.Less(x, max);
  99. return x;
  100. }
  101. [TestCompiler]
  102. public static void bool_uniform()
  103. {
  104. var rnd = new Random(0x6E624EB7u);
  105. ks_test((() => rnd.NextBool() ? 0.75 : 0.25), 2);
  106. }
  107. [TestCompiler]
  108. public static void bool2_uniform()
  109. {
  110. var rnd = new Random(0x6E624EB7u);
  111. ks_test((() => rnd.NextBool2().x ? 0.75 : 0.25), 2);
  112. ks_test((() => rnd.NextBool2().y ? 0.75 : 0.25), 2);
  113. }
  114. [TestCompiler]
  115. public static void bool2_independent()
  116. {
  117. var rnd = new Random(0x6E624EB7u);
  118. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool2().xy)));
  119. }
  120. [TestCompiler]
  121. public static void bool3_uniform()
  122. {
  123. var rnd = new Random(0x6E624EB7u);
  124. ks_test((() => rnd.NextBool3().x ? 0.75 : 0.25), 2);
  125. ks_test((() => rnd.NextBool3().y ? 0.75 : 0.25), 2);
  126. ks_test((() => rnd.NextBool3().z ? 0.75 : 0.25), 2);
  127. }
  128. [TestCompiler]
  129. public static void bool3_independent()
  130. {
  131. var rnd = new Random(0x6E624EB7u);
  132. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool3().xy)));
  133. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool3().xz)));
  134. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool3().yz)));
  135. }
  136. [TestCompiler]
  137. public static void bool4_uniform()
  138. {
  139. var rnd = new Random(0x6E624EB7u);
  140. ks_test((() => rnd.NextBool4().x ? 0.75 : 0.25), 2);
  141. ks_test((() => rnd.NextBool4().y ? 0.75 : 0.25), 2);
  142. ks_test((() => rnd.NextBool4().z ? 0.75 : 0.25), 2);
  143. ks_test((() => rnd.NextBool4().w ? 0.75 : 0.25), 2);
  144. }
  145. [TestCompiler]
  146. public static void bool4_independent()
  147. {
  148. var rnd = new Random(0x6E624EB7u);
  149. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool4().xy)));
  150. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool4().xz)));
  151. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool4().xw)));
  152. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool4().yz)));
  153. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool4().yw)));
  154. r_test((() => select(double2(0.25), double2(0.75), rnd.NextBool4().zw)));
  155. }
  156. [TestCompiler]
  157. public static void int_uniform_low_bits()
  158. {
  159. var rnd = new Random(0x6E624EB7u);
  160. ks_test((() => ((rnd.NextInt() & 255u) + 0.5) / 256.0), 256);
  161. }
  162. [TestCompiler]
  163. public static void int_uniform_high_bits()
  164. {
  165. var rnd = new Random(0x6E624EB7u);
  166. ks_test((() => (((uint)rnd.NextInt() >> 24) + 0.5) / 256.0), 256);
  167. }
  168. [TestCompiler]
  169. public static void int_uniform_max()
  170. {
  171. var rnd = new Random(0x6E624EB7u);
  172. int max = 17;
  173. ks_test((() => (range_check(rnd.NextInt(max), 0, max) + 0.5) / max), max);
  174. }
  175. [TestCompiler]
  176. public static void int_uniform_max_limit()
  177. {
  178. var rnd = new Random(0x6E624EB7u);
  179. int max = 2147483647;
  180. ks_test((() => (range_check(rnd.NextInt(max), 0, max) + 0.5) / max), 256);
  181. }
  182. [TestCompiler]
  183. public static void int_uniform_min_max()
  184. {
  185. var rnd = new Random(0x6E624EB7u);
  186. int min = -7;
  187. int max = 17;
  188. int range = max - min;
  189. ks_test((() => (range_check(rnd.NextInt(min, max), min, max) + 0.5 - min) / range), range);
  190. }
  191. [TestCompiler]
  192. public static void int_uniform_min_max_limit()
  193. {
  194. var rnd = new Random(0x6E624EB7u);
  195. int min = -2147483648;
  196. int max = 2147483647;
  197. long range = (long)max - (long)min;
  198. ks_test((() => (range_check(rnd.NextInt(min, max), min, max) + 0.5 - min) / range), 256);
  199. }
  200. [TestCompiler]
  201. public static void int2_uniform_low_bits()
  202. {
  203. var rnd = new Random(0x6E624EB7u);
  204. ks_test((() => ((rnd.NextInt2().x & 255u) + 0.5) / 256.0), 256);
  205. ks_test((() => ((rnd.NextInt2().y & 255u) + 0.5) / 256.0), 256);
  206. }
  207. [TestCompiler]
  208. public static void int2_uniform_high_bits()
  209. {
  210. var rnd = new Random(0x6E624EB7u);
  211. ks_test((() => (((uint)rnd.NextInt2().x >> 24) + 0.5) / 256.0), 256);
  212. ks_test((() => (((uint)rnd.NextInt2().y >> 24) + 0.5) / 256.0), 256);
  213. }
  214. [TestCompiler]
  215. public static void int2_uniform_max()
  216. {
  217. var rnd = new Random(0x6E624EB7u);
  218. int max = 2147483647;
  219. ks_test((() => (range_check(rnd.NextInt2(max).x, 0, max) + 0.5) / max), 256);
  220. ks_test((() => (range_check(rnd.NextInt2(max).y, 0, max) + 0.5) / max), 256);
  221. }
  222. [TestCompiler]
  223. public static void int2_uniform_min_max()
  224. {
  225. var rnd = new Random(0x6E624EB7u);
  226. int2 min = int2(-7, 3);
  227. int2 max = int2(17, 14);
  228. int2 range = max - min;
  229. ks_test((() => (range_check(rnd.NextInt2(min, max).x, min.x, max.x) + 0.5 - min.x) / range.x), range.x);
  230. ks_test((() => (range_check(rnd.NextInt2(min, max).y, min.y, max.y) + 0.5 - min.y) / range.y), range.y);
  231. }
  232. [TestCompiler]
  233. public static void int2_uniform_min_max_limit()
  234. {
  235. var rnd = new Random(0x6E624EB7u);
  236. int min = -2147483648;
  237. int max = 2147483647;
  238. long range = (long)max - (long)min;
  239. ks_test((() => (range_check(rnd.NextInt2(min, max).x, min, max) + 0.5 - min) / range), 256);
  240. ks_test((() => (range_check(rnd.NextInt2(min, max).y, min, max) + 0.5 - min) / range), 256);
  241. }
  242. [TestCompiler]
  243. public static void int2_independent_low_bits()
  244. {
  245. var rnd = new Random(0x6E624EB7u);
  246. r_test(() => (rnd.NextInt2().xy & 255));
  247. }
  248. [TestCompiler]
  249. public static void int2_independent_high_bits()
  250. {
  251. var rnd = new Random(0x6E624EB7u);
  252. r_test(() => ((uint2)rnd.NextInt2().xy >> 24));
  253. }
  254. [TestCompiler]
  255. public static void int3_uniform_low_bits()
  256. {
  257. var rnd = new Random(0x6E624EB7u);
  258. ks_test((() => ((rnd.NextInt3().x & 255u) + 0.5) / 256.0), 256);
  259. ks_test((() => ((rnd.NextInt3().y & 255u) + 0.5) / 256.0), 256);
  260. ks_test((() => ((rnd.NextInt3().z & 255u) + 0.5) / 256.0), 256);
  261. }
  262. [TestCompiler]
  263. public static void int3_uniform_high_bits()
  264. {
  265. var rnd = new Random(0x6E624EB7u);
  266. ks_test((() => (((uint)rnd.NextInt3().x >> 24) + 0.5) / 256.0), 256);
  267. ks_test((() => (((uint)rnd.NextInt3().y >> 24) + 0.5) / 256.0), 256);
  268. ks_test((() => (((uint)rnd.NextInt3().z >> 24) + 0.5) / 256.0), 256);
  269. }
  270. [TestCompiler]
  271. public static void int3_uniform_max()
  272. {
  273. var rnd = new Random(0x6E624EB7u);
  274. int3 max = int3(13, 17, 19);
  275. ks_test((() => ((uint)range_check(rnd.NextInt3(max).x, 0, max.x) + 0.5) / max.x), max.x);
  276. ks_test((() => ((uint)range_check(rnd.NextInt3(max).y, 0, max.y) + 0.5) / max.y), max.y);
  277. ks_test((() => ((uint)range_check(rnd.NextInt3(max).z, 0, max.z) + 0.5) / max.z), max.z);
  278. }
  279. [TestCompiler]
  280. public static void int3_uniform_max_limit()
  281. {
  282. var rnd = new Random(0x6E624EB7u);
  283. int max = 2147483647;
  284. ks_test((() => ((uint)range_check(rnd.NextInt3(max).x, 0, max) + 0.5) / max), 256);
  285. ks_test((() => ((uint)range_check(rnd.NextInt3(max).y, 0, max) + 0.5) / max), 256);
  286. ks_test((() => ((uint)range_check(rnd.NextInt3(max).z, 0, max) + 0.5) / max), 256);
  287. }
  288. [TestCompiler]
  289. public static void int3_uniform_min_max()
  290. {
  291. var rnd = new Random(0x6E624EB7u);
  292. int3 min = int3(-7, 3, -10);
  293. int3 max = int3(17, 14, -3);
  294. int3 range = max - min;
  295. ks_test((() => (range_check(rnd.NextInt3(min, max).x, min.x, max.x) + 0.5 - min.x) / range.x), range.x);
  296. ks_test((() => (range_check(rnd.NextInt3(min, max).y, min.y, max.y) + 0.5 - min.y) / range.y), range.y);
  297. ks_test((() => (range_check(rnd.NextInt3(min, max).z, min.z, max.z) + 0.5 - min.z) / range.z), range.z);
  298. }
  299. [TestCompiler]
  300. public static void int3_uniform_min_max_limit()
  301. {
  302. var rnd = new Random(0x6E624EB7u);
  303. int min = -2147483648;
  304. int max = 2147483647;
  305. long range = (long)max - (long)min;
  306. ks_test((() => (range_check(rnd.NextInt3(min, max).x, min, max) + 0.5 - min) / range), 256);
  307. ks_test((() => (range_check(rnd.NextInt3(min, max).y, min, max) + 0.5 - min) / range), 256);
  308. ks_test((() => (range_check(rnd.NextInt3(min, max).z, min, max) + 0.5 - min) / range), 256);
  309. }
  310. [TestCompiler]
  311. public static void int3_independent_low_bits()
  312. {
  313. var rnd = new Random(0x6E624EB7u);
  314. r_test(() => (rnd.NextInt3().xy & 255));
  315. r_test(() => (rnd.NextInt3().xz & 255));
  316. r_test(() => (rnd.NextInt3().yz & 255));
  317. }
  318. [TestCompiler]
  319. public static void int3_independent_high_bits()
  320. {
  321. var rnd = new Random(0x6E624EB7u);
  322. r_test(() => ((uint2)rnd.NextInt3().xy >> 24));
  323. r_test(() => ((uint2)rnd.NextInt3().xz >> 24));
  324. r_test(() => ((uint2)rnd.NextInt3().yz >> 24));
  325. }
  326. [TestCompiler]
  327. public static void int4_uniform_low_bits()
  328. {
  329. var rnd = new Random(0x6E624EB7u);
  330. ks_test((() => ((rnd.NextInt4().x & 255u) + 0.5) / 256.0), 256);
  331. ks_test((() => ((rnd.NextInt4().y & 255u) + 0.5) / 256.0), 256);
  332. ks_test((() => ((rnd.NextInt4().z & 255u) + 0.5) / 256.0), 256);
  333. ks_test((() => ((rnd.NextInt4().w & 255u) + 0.5) / 256.0), 256);
  334. }
  335. [TestCompiler]
  336. public static void int4_uniform_high_bits()
  337. {
  338. var rnd = new Random(0x6E624EB7u);
  339. ks_test((() => (((uint)rnd.NextInt4().x >> 24) + 0.5) / 256.0), 256);
  340. ks_test((() => (((uint)rnd.NextInt4().y >> 24) + 0.5) / 256.0), 256);
  341. ks_test((() => (((uint)rnd.NextInt4().z >> 24) + 0.5) / 256.0), 256);
  342. ks_test((() => (((uint)rnd.NextInt4().w >> 24) + 0.5) / 256.0), 256);
  343. }
  344. [TestCompiler]
  345. public static void int4_uniform_max()
  346. {
  347. var rnd = new Random(0x6E624EB7u);
  348. int4 max = int4(13, 17, 19, 23);
  349. ks_test((() => (range_check(rnd.NextInt4(max).x, 0, max.x) + 0.5) / max.x), max.x);
  350. ks_test((() => (range_check(rnd.NextInt4(max).y, 0, max.y) + 0.5) / max.y), max.y);
  351. ks_test((() => (range_check(rnd.NextInt4(max).z, 0, max.z) + 0.5) / max.z), max.z);
  352. ks_test((() => (range_check(rnd.NextInt4(max).w, 0, max.w) + 0.5) / max.w), max.w);
  353. }
  354. [TestCompiler]
  355. public static void int4_uniform_max_limit()
  356. {
  357. var rnd = new Random(0x6E624EB7u);
  358. int max = 2147483647;
  359. ks_test((() => (range_check(rnd.NextInt4(max).x, 0, max) + 0.5) / max), 256);
  360. ks_test((() => (range_check(rnd.NextInt4(max).y, 0, max) + 0.5) / max), 256);
  361. ks_test((() => (range_check(rnd.NextInt4(max).z, 0, max) + 0.5) / max), 256);
  362. ks_test((() => (range_check(rnd.NextInt4(max).w, 0, max) + 0.5) / max), 256);
  363. }
  364. [TestCompiler]
  365. public static void int4_uniform_min_max()
  366. {
  367. var rnd = new Random(0x6E624EB7u);
  368. int4 min = int4(-7, 3, -10, 1);
  369. int4 max = int4(17, 14, -3, 111);
  370. int4 range = max - min;
  371. ks_test((() => (range_check(rnd.NextInt4(min, max).x, min.x, max.x) + 0.5 - min.x) / range.x), range.x);
  372. ks_test((() => (range_check(rnd.NextInt4(min, max).y, min.y, max.y) + 0.5 - min.y) / range.y), range.y);
  373. ks_test((() => (range_check(rnd.NextInt4(min, max).z, min.z, max.z) + 0.5 - min.z) / range.z), range.z);
  374. ks_test((() => (range_check(rnd.NextInt4(min, max).w, min.w, max.w) + 0.5 - min.w) / range.w), range.w);
  375. }
  376. [TestCompiler]
  377. public static void int4_uniform_min_max_limit()
  378. {
  379. var rnd = new Random(0x6E624EB7u);
  380. int min = -2147483648;
  381. int max = 2147483647;
  382. long range = (long)max - (long)min;
  383. ks_test((() => (range_check(rnd.NextInt4(min, max).x, min, max) + 0.5 - min) / range), 256);
  384. ks_test((() => (range_check(rnd.NextInt4(min, max).y, min, max) + 0.5 - min) / range), 256);
  385. ks_test((() => (range_check(rnd.NextInt4(min, max).z, min, max) + 0.5 - min) / range), 256);
  386. ks_test((() => (range_check(rnd.NextInt4(min, max).w, min, max) + 0.5 - min) / range), 256);
  387. }
  388. [TestCompiler]
  389. public static void int4_independent_low_bits()
  390. {
  391. var rnd = new Random(0x6E624EB7u);
  392. r_test(() => (rnd.NextUInt4().xy & 255));
  393. r_test(() => (rnd.NextUInt4().xz & 255));
  394. r_test(() => (rnd.NextUInt4().xw & 255));
  395. r_test(() => (rnd.NextUInt4().yz & 255));
  396. r_test(() => (rnd.NextUInt4().yw & 255));
  397. r_test(() => (rnd.NextUInt4().zw & 255));
  398. }
  399. [TestCompiler]
  400. public static void int4_independent_high_bits()
  401. {
  402. var rnd = new Random(0x6E624EB7u);
  403. r_test(() => ((uint2)rnd.NextUInt4().xy >> 24));
  404. r_test(() => ((uint2)rnd.NextUInt4().xz >> 24));
  405. r_test(() => ((uint2)rnd.NextUInt4().xw >> 24));
  406. r_test(() => ((uint2)rnd.NextUInt4().yz >> 24));
  407. r_test(() => ((uint2)rnd.NextUInt4().yw >> 24));
  408. r_test(() => ((uint2)rnd.NextUInt4().zw >> 24));
  409. }
  410. [TestCompiler]
  411. public static void uint_uniform_low_bits()
  412. {
  413. var rnd = new Random(0x6E624EB7u);
  414. ks_test((() => ((rnd.NextUInt() & 255u) + 0.5) / 256.0), 256);
  415. }
  416. [TestCompiler]
  417. public static void uint_uniform_high_bits()
  418. {
  419. var rnd = new Random(0x6E624EB7u);
  420. ks_test((() => ((rnd.NextUInt() >> 24) + 0.5) / 256.0), 256);
  421. }
  422. [TestCompiler]
  423. public static void uint_uniform_max()
  424. {
  425. var rnd = new Random(0x6E624EB7u);
  426. uint max = 17;
  427. ks_test((() => (rnd.NextUInt(max) + 0.5) / max), (int)max);
  428. }
  429. [TestCompiler]
  430. public static void uint_uniform_max_limit()
  431. {
  432. var rnd = new Random(0x6E624EB7u);
  433. uint max = 0xFFFFFFFF;
  434. ks_test((() => (rnd.NextUInt(max) + 0.5) / max), 256);
  435. }
  436. [TestCompiler]
  437. public static void uint_uniform_min_max()
  438. {
  439. var rnd = new Random(0x6E624EB7u);
  440. uint min = 3;
  441. uint max = 17;
  442. uint range = max - min;
  443. ks_test((() => (range_check(rnd.NextUInt(min, max), min, max) + 0.5 - min) / range), (int)range);
  444. }
  445. [TestCompiler]
  446. public static void uint_uniform_min_max_limit()
  447. {
  448. var rnd = new Random(0x6E624EB7u);
  449. uint max = 0xFFFFFFFF;
  450. ks_test((() => (range_check(rnd.NextUInt(0, max), 0, max) + 0.5) / max), 256);
  451. }
  452. [TestCompiler]
  453. public static void uint2_uniform_low_bits()
  454. {
  455. var rnd = new Random(0x6E624EB7u);
  456. ks_test((() => ((rnd.NextUInt2().x & 255u) + 0.5) / 256.0), 256);
  457. ks_test((() => ((rnd.NextUInt2().y & 255u) + 0.5) / 256.0), 256);
  458. }
  459. [TestCompiler]
  460. public static void uint2_uniform_high_bits()
  461. {
  462. var rnd = new Random(0x6E624EB7u);
  463. ks_test((() => ((rnd.NextUInt2().x >> 24) + 0.5) / 256.0), 256);
  464. ks_test((() => ((rnd.NextUInt2().y >> 24) + 0.5) / 256.0), 256);
  465. }
  466. [TestCompiler]
  467. public static void uint2_uniform_max()
  468. {
  469. var rnd = new Random(0x6E624EB7u);
  470. uint2 max = uint2(13, 17);
  471. ks_test((() => (rnd.NextUInt2(max).x + 0.5) / max.x), (int)max.x);
  472. ks_test((() => (rnd.NextUInt2(max).y + 0.5) / max.y), (int)max.y);
  473. }
  474. [TestCompiler]
  475. public static void uint2_uniform_max_limit()
  476. {
  477. var rnd = new Random(0x6E624EB7u);
  478. uint max = 0xFFFFFFFF;
  479. ks_test((() => (rnd.NextUInt2(max).x + 0.5) / max), 256);
  480. ks_test((() => (rnd.NextUInt2(max).y + 0.5) / max), 256);
  481. }
  482. [TestCompiler]
  483. public static void uint2_uniform_min_max()
  484. {
  485. var rnd = new Random(0x6E624EB7u);
  486. uint2 min = uint2(3, 101);
  487. uint2 max = uint2(17, 117);
  488. uint2 range = max - min;
  489. ks_test((() => (range_check(rnd.NextUInt2(min, max).x, min.x, max.x) + 0.5 - min.x) / range.x), (int)range.x);
  490. ks_test((() => (range_check(rnd.NextUInt2(min, max).y, min.y, max.y) + 0.5 - min.y) / range.y), (int)range.y);
  491. }
  492. [TestCompiler]
  493. public static void uint2_uniform_min_max_limit()
  494. {
  495. var rnd = new Random(0x6E624EB7u);
  496. uint max = 0xFFFFFFFF;
  497. ks_test((() => (range_check(rnd.NextUInt2(0, max).x, 0, max) + 0.5) / max), 256);
  498. ks_test((() => (range_check(rnd.NextUInt2(0, max).y, 0, max) + 0.5) / max), 256);
  499. }
  500. [TestCompiler]
  501. public static void uint2_independent_low_bits()
  502. {
  503. var rnd = new Random(0x6E624EB7u);
  504. r_test(() => (rnd.NextUInt2().xy & 255));
  505. }
  506. [TestCompiler]
  507. public static void uint2_independent_high_bits()
  508. {
  509. var rnd = new Random(0x6E624EB7u);
  510. r_test(() => (rnd.NextUInt2().xy >> 24));
  511. }
  512. [TestCompiler]
  513. public static void uint3_uniform_low_bits()
  514. {
  515. var rnd = new Random(0x6E624EB7u);
  516. ks_test((() => ((rnd.NextUInt3().x & 255u) + 0.5) / 256.0), 256);
  517. ks_test((() => ((rnd.NextUInt3().y & 255u) + 0.5) / 256.0), 256);
  518. ks_test((() => ((rnd.NextUInt3().z & 255u) + 0.5) / 256.0), 256);
  519. }
  520. [TestCompiler]
  521. public static void uint3_uniform_high_bits()
  522. {
  523. var rnd = new Random(0x6E624EB7u);
  524. ks_test((() => ((rnd.NextUInt3().x >> 24) + 0.5) / 256.0), 256);
  525. ks_test((() => ((rnd.NextUInt3().y >> 24) + 0.5) / 256.0), 256);
  526. ks_test((() => ((rnd.NextUInt3().z >> 24) + 0.5) / 256.0), 256);
  527. }
  528. [TestCompiler]
  529. public static void uint3_uniform_max()
  530. {
  531. var rnd = new Random(0x6E624EB7u);
  532. uint3 max = uint3(13, 17, 19);
  533. ks_test((() => (rnd.NextUInt3(max).x + 0.5) / max.x), (int)max.x);
  534. ks_test((() => (rnd.NextUInt3(max).y + 0.5) / max.y), (int)max.y);
  535. ks_test((() => (rnd.NextUInt3(max).z + 0.5) / max.z), (int)max.z);
  536. }
  537. [TestCompiler]
  538. public static void uint3_uniform_max_limit()
  539. {
  540. var rnd = new Random(0x6E624EB7u);
  541. uint max = 0xFFFFFFFF;
  542. ks_test((() => (rnd.NextUInt3(max).x + 0.5) / max), 256);
  543. ks_test((() => (rnd.NextUInt3(max).y + 0.5) / max), 256);
  544. ks_test((() => (rnd.NextUInt3(max).z + 0.5) / max), 256);
  545. }
  546. [TestCompiler]
  547. public static void uint3_uniform_min_max()
  548. {
  549. var rnd = new Random(0x6E624EB7u);
  550. uint3 min = uint3(3, 101, 0xFFFFFFF0);
  551. uint3 max = uint3(17, 117, 0xFFFFFFFF);
  552. uint3 range = max - min;
  553. ks_test((() => (range_check(rnd.NextUInt3(min, max).x, min.x, max.x) + 0.5 - min.x) / range.x), (int)range.x);
  554. ks_test((() => (range_check(rnd.NextUInt3(min, max).y, min.y, max.y) + 0.5 - min.y) / range.y), (int)range.y);
  555. ks_test((() => (range_check(rnd.NextUInt3(min, max).z, min.z, max.z) + 0.5 - min.z) / range.z), (int)range.z);
  556. }
  557. [TestCompiler]
  558. public static void uint3_uniform_min_max_limit()
  559. {
  560. var rnd = new Random(0x6E624EB7u);
  561. uint max = 0xFFFFFFFF;
  562. ks_test((() => (range_check(rnd.NextUInt3(0, max).x, 0, max) + 0.5) / max), 256);
  563. ks_test((() => (range_check(rnd.NextUInt3(0, max).y, 0, max) + 0.5) / max), 256);
  564. ks_test((() => (range_check(rnd.NextUInt3(0, max).z, 0, max) + 0.5) / max), 256);
  565. }
  566. [TestCompiler]
  567. public static void uint3_independent_low_bits()
  568. {
  569. var rnd = new Random(0x6E624EB7u);
  570. r_test(() => (rnd.NextUInt3().xy & 255));
  571. r_test(() => (rnd.NextUInt3().xz & 255));
  572. r_test(() => (rnd.NextUInt3().yz & 255));
  573. }
  574. [TestCompiler]
  575. public static void uint3_independent_high_bits()
  576. {
  577. var rnd = new Random(0x6E624EB7u);
  578. r_test(() => (rnd.NextUInt3().xy >> 24));
  579. r_test(() => (rnd.NextUInt3().xz >> 24));
  580. r_test(() => (rnd.NextUInt3().yz >> 24));
  581. }
  582. [TestCompiler]
  583. public static void uint4_uniform_low_bits()
  584. {
  585. var rnd = new Random(0x6E624EB7u);
  586. ks_test((() => ((rnd.NextUInt4().x & 255u) + 0.5) / 256.0), 256);
  587. ks_test((() => ((rnd.NextUInt4().y & 255u) + 0.5) / 256.0), 256);
  588. ks_test((() => ((rnd.NextUInt4().z & 255u) + 0.5) / 256.0), 256);
  589. ks_test((() => ((rnd.NextUInt4().w & 255u) + 0.5) / 256.0), 256);
  590. }
  591. [TestCompiler]
  592. public static void uint4_uniform_high_bits()
  593. {
  594. var rnd = new Random(0x6E624EB7u);
  595. ks_test((() => ((rnd.NextUInt4().x >> 24) + 0.5) / 256.0), 256);
  596. ks_test((() => ((rnd.NextUInt4().y >> 24) + 0.5) / 256.0), 256);
  597. ks_test((() => ((rnd.NextUInt4().z >> 24) + 0.5) / 256.0), 256);
  598. ks_test((() => ((rnd.NextUInt4().w >> 24) + 0.5) / 256.0), 256);
  599. }
  600. [TestCompiler]
  601. public static void uint4_uniform_max()
  602. {
  603. var rnd = new Random(0x6E624EB7u);
  604. uint4 max = uint4(13, 17, 19, 23);
  605. ks_test((() => (rnd.NextUInt4(max).x + 0.5) / max.x), (int)max.x);
  606. ks_test((() => (rnd.NextUInt4(max).y + 0.5) / max.y), (int)max.y);
  607. ks_test((() => (rnd.NextUInt4(max).z + 0.5) / max.z), (int)max.z);
  608. ks_test((() => (rnd.NextUInt4(max).w + 0.5) / max.w), (int)max.w);
  609. }
  610. [TestCompiler]
  611. public static void uint4_uniform_max_limit()
  612. {
  613. var rnd = new Random(0x6E624EB7u);
  614. uint max = 0xFFFFFFFF;
  615. ks_test((() => (rnd.NextUInt4(max).x + 0.5) / max), 256);
  616. ks_test((() => (rnd.NextUInt4(max).y + 0.5) / max), 256);
  617. ks_test((() => (rnd.NextUInt4(max).z + 0.5) / max), 256);
  618. ks_test((() => (rnd.NextUInt4(max).w + 0.5) / max), 256);
  619. }
  620. [TestCompiler]
  621. public static void uint4_uniform_min_max()
  622. {
  623. var rnd = new Random(0x6E624EB7u);
  624. uint4 min = uint4(3, 101, 0xFFFFFFF0, 100);
  625. uint4 max = uint4(17, 117, 0xFFFFFFFF, 1000);
  626. uint4 range = max - min;
  627. ks_test((() => (range_check(rnd.NextUInt4(min, max).x, min.x, max.x) + 0.5 - min.x) / range.x), (int)range.x);
  628. ks_test((() => (range_check(rnd.NextUInt4(min, max).y, min.y, max.y) + 0.5 - min.y) / range.y), (int)range.y);
  629. ks_test((() => (range_check(rnd.NextUInt4(min, max).z, min.z, max.z) + 0.5 - min.z) / range.z), (int)range.z);
  630. ks_test((() => (range_check(rnd.NextUInt4(min, max).w, min.w, max.w) + 0.5 - min.w) / range.w), (int)range.w);
  631. }
  632. [TestCompiler]
  633. public static void uint4_uniform_min_max_limit()
  634. {
  635. var rnd = new Random(0x6E624EB7u);
  636. uint max = 0xFFFFFFFF;
  637. ks_test((() => (range_check(rnd.NextUInt4(0, max).x, 0, max) + 0.5) / max), 256);
  638. ks_test((() => (range_check(rnd.NextUInt4(0, max).y, 0, max) + 0.5) / max), 256);
  639. ks_test((() => (range_check(rnd.NextUInt4(0, max).z, 0, max) + 0.5) / max), 256);
  640. ks_test((() => (range_check(rnd.NextUInt4(0, max).w, 0, max) + 0.5) / max), 256);
  641. }
  642. [TestCompiler]
  643. public static void uint4_independent_low_bits()
  644. {
  645. var rnd = new Random(0x6E624EB7u);
  646. r_test(() => (rnd.NextUInt4().xy & 255));
  647. r_test(() => (rnd.NextUInt4().xz & 255));
  648. r_test(() => (rnd.NextUInt4().xw & 255));
  649. r_test(() => (rnd.NextUInt4().yz & 255));
  650. r_test(() => (rnd.NextUInt4().yw & 255));
  651. r_test(() => (rnd.NextUInt4().zw & 255));
  652. }
  653. [TestCompiler]
  654. public static void uint4_independent_high_bits()
  655. {
  656. var rnd = new Random(0x6E624EB7u);
  657. r_test(() => (rnd.NextUInt4().xy >> 24));
  658. r_test(() => (rnd.NextUInt4().xz >> 24));
  659. r_test(() => (rnd.NextUInt4().xw >> 24));
  660. r_test(() => (rnd.NextUInt4().yz >> 24));
  661. r_test(() => (rnd.NextUInt4().yw >> 24));
  662. r_test(() => (rnd.NextUInt4().zw >> 24));
  663. }
  664. [TestCompiler]
  665. public static void float_uniform()
  666. {
  667. var rnd = new Random(0x6E624EB7u);
  668. ks_test((() => range_check01(rnd.NextFloat())));
  669. }
  670. [TestCompiler]
  671. public static void float_uniform_low_bits()
  672. {
  673. var rnd = new Random(0x6E624EB7u);
  674. ks_test((() => frac(rnd.NextFloat() * 65536.0f)));
  675. }
  676. [TestCompiler]
  677. public static void float_uniform_max()
  678. {
  679. var rnd = new Random(0x6E624EB7u);
  680. float max = 16.4f;
  681. ks_test((() => range_check(rnd.NextFloat(max), 0.0f, max) / max));
  682. }
  683. [TestCompiler]
  684. public static void float_uniform_min_max()
  685. {
  686. var rnd = new Random(0x6E624EB7u);
  687. float min = -3.1f;
  688. float max = 16.4f;
  689. float range = max - min;
  690. ks_test((() => (range_check(rnd.NextFloat(min, max), min, max) -min) / range));
  691. }
  692. [TestCompiler]
  693. public static void float2_uniform()
  694. {
  695. var rnd = new Random(0x6E624EB7u);
  696. ks_test((() => range_check01(rnd.NextFloat2().x)));
  697. ks_test((() => range_check01(rnd.NextFloat2().y)));
  698. }
  699. [TestCompiler]
  700. public static void float2_uniform_low_bits()
  701. {
  702. var rnd = new Random(0x6E624EB7u);
  703. ks_test((() => frac(rnd.NextFloat2().x * 65536.0f)));
  704. ks_test((() => frac(rnd.NextFloat2().y * 65536.0f)));
  705. }
  706. [TestCompiler]
  707. public static void float2_uniform_max()
  708. {
  709. var rnd = new Random(0x6E624EB7u);
  710. float2 max = float2(16.4f, 1001.33333333f);
  711. ks_test((() => range_check(rnd.NextFloat2(max).x, 0.0f, max.x) / max.x));
  712. ks_test((() => range_check(rnd.NextFloat2(max).y, 0.0f, max.y) / max.y));
  713. }
  714. [TestCompiler]
  715. public static void float2_uniform_min_max()
  716. {
  717. var rnd = new Random(0x6E624EB7u);
  718. float2 min = float2(-3.1f, 17.1f);
  719. float2 max = float2(16.4f, 1001.33333333f);
  720. float2 range = max - min;
  721. ks_test((() => (range_check(rnd.NextFloat2(min, max).x, min.x, max.x) - min.x) / range.x));
  722. ks_test((() => (range_check(rnd.NextFloat2(min, max).y, min.y, max.y) - min.y) / range.y));
  723. }
  724. [TestCompiler]
  725. public static void float2_independent()
  726. {
  727. var rnd = new Random(0x6E624EB7u);
  728. r_test((() => rnd.NextFloat2()));
  729. }
  730. [TestCompiler]
  731. public static void float3_uniform()
  732. {
  733. var rnd = new Random(0x6E624EB7u);
  734. ks_test((() => range_check01(rnd.NextFloat3().x)));
  735. ks_test((() => range_check01(rnd.NextFloat3().y)));
  736. ks_test((() => range_check01(rnd.NextFloat3().z)));
  737. }
  738. [TestCompiler]
  739. public static void float3_uniform_low_bits()
  740. {
  741. var rnd = new Random(0x6E624EB7u);
  742. ks_test((() => frac(rnd.NextFloat3().x * 65536.0f)));
  743. ks_test((() => frac(rnd.NextFloat3().y * 65536.0f)));
  744. ks_test((() => frac(rnd.NextFloat3().z * 65536.0f)));
  745. }
  746. [TestCompiler]
  747. public static void float3_uniform_max()
  748. {
  749. var rnd = new Random(0x6E624EB7u);
  750. float3 max = float3(16.4f, 1001.33333333f, 3.121f);
  751. ks_test((() => range_check(rnd.NextFloat3(max).x, 0.0f, max.x) / max.x));
  752. ks_test((() => range_check(rnd.NextFloat3(max).y, 0.0f, max.y) / max.y));
  753. ks_test((() => range_check(rnd.NextFloat3(max).z, 0.0f, max.z) / max.z));
  754. }
  755. [TestCompiler]
  756. public static void float3_uniform_min_max()
  757. {
  758. var rnd = new Random(0x6E624EB7u);
  759. float3 min = float3(-3.1f, 17.1f, -0.3f);
  760. float3 max = float3(16.4f, 1001.33333333f, 3.121f);
  761. float3 range = max - min;
  762. ks_test((() => (range_check(rnd.NextFloat3(min, max).x, min.x, max.x) - min.x) / range.x));
  763. ks_test((() => (range_check(rnd.NextFloat3(min, max).y, min.y, max.y) - min.y) / range.y));
  764. ks_test((() => (range_check(rnd.NextFloat3(min, max).z, min.z, max.z) - min.z) / range.z));
  765. }
  766. [TestCompiler]
  767. public static void float3_independent()
  768. {
  769. var rnd = new Random(0x6E624EB7u);
  770. r_test((() => rnd.NextFloat3().xy));
  771. r_test((() => rnd.NextFloat3().xz));
  772. r_test((() => rnd.NextFloat3().yz));
  773. }
  774. [TestCompiler]
  775. public static void float4_uniform()
  776. {
  777. var rnd = new Random(0x6E624EB7u);
  778. ks_test((() => range_check01(rnd.NextFloat4().x)));
  779. ks_test((() => range_check01(rnd.NextFloat4().y)));
  780. ks_test((() => range_check01(rnd.NextFloat4().z)));
  781. ks_test((() => range_check01(rnd.NextFloat4().w)));
  782. }
  783. [TestCompiler]
  784. public static void float4_uniform_low_bits()
  785. {
  786. var rnd = new Random(0x6E624EB7u);
  787. ks_test((() => frac(rnd.NextFloat4().x * 65536.0f)));
  788. ks_test((() => frac(rnd.NextFloat4().y * 65536.0f)));
  789. ks_test((() => frac(rnd.NextFloat4().z * 65536.0f)));
  790. ks_test((() => frac(rnd.NextFloat4().w * 65536.0f)));
  791. }
  792. [TestCompiler]
  793. public static void float4_uniform_max()
  794. {
  795. var rnd = new Random(0x6E624EB7u);
  796. float4 max = float4(16.4f, 1001.33333333f, 3.121f, 1.3232e23f);
  797. ks_test((() => range_check(rnd.NextFloat4(max).x, 0.0f, max.x) / max.x));
  798. ks_test((() => range_check(rnd.NextFloat4(max).y, 0.0f, max.y) / max.y));
  799. ks_test((() => range_check(rnd.NextFloat4(max).z, 0.0f, max.z) / max.z));
  800. ks_test((() => range_check(rnd.NextFloat4(max).w, 0.0f, max.w) / max.w));
  801. }
  802. [TestCompiler]
  803. public static void float4_uniform_min_max()
  804. {
  805. var rnd = new Random(0x6E624EB7u);
  806. float4 min = float4(-3.1f, 17.1f, -0.3f, -22.6f);
  807. float4 max = float4(16.4f, 1001.33333333f, 3.121f, 1.3232e23f);
  808. float4 range = max - min;
  809. ks_test((() => (range_check(rnd.NextFloat4(min, max).x, min.x, max.x) - min.x) / range.x));
  810. ks_test((() => (range_check(rnd.NextFloat4(min, max).y, min.y, max.y) - min.y) / range.y));
  811. ks_test((() => (range_check(rnd.NextFloat4(min, max).z, min.z, max.z) - min.z) / range.z));
  812. ks_test((() => (range_check(rnd.NextFloat4(min, max).w, min.w, max.w) - min.w) / range.w));
  813. }
  814. [TestCompiler]
  815. public static void float4_independent()
  816. {
  817. var rnd = new Random(0x6E624EB7u);
  818. r_test((() => rnd.NextFloat4().xy));
  819. r_test((() => rnd.NextFloat4().xz));
  820. r_test((() => rnd.NextFloat4().xw));
  821. r_test((() => rnd.NextFloat4().yz));
  822. r_test((() => rnd.NextFloat4().yw));
  823. r_test((() => rnd.NextFloat4().zw));
  824. }
  825. [TestCompiler]
  826. public static void double_uniform()
  827. {
  828. var rnd = new Random(0x6E624EB7u);
  829. ks_test((() => range_check01(rnd.NextDouble())));
  830. }
  831. [TestCompiler]
  832. public static void double_uniform_low_bits()
  833. {
  834. var rnd = new Random(0x6E624EB7u);
  835. ks_test((() => frac(rnd.NextDouble() * 35184372088832.0)));
  836. }
  837. [TestCompiler]
  838. public static void double_uniform_max()
  839. {
  840. var rnd = new Random(0x6E624EB7u);
  841. double max = 16.4;
  842. ks_test((() => range_check(rnd.NextDouble(max), 0.0, max) / max));
  843. }
  844. [TestCompiler]
  845. public static void double_uniform_min_max()
  846. {
  847. var rnd = new Random(0x6E624EB7u);
  848. double min = -3.1;
  849. double max = 16.4;
  850. double range = max - min;
  851. ks_test((() => (range_check(rnd.NextDouble(min, max), min, max) - min) / range));
  852. }
  853. [TestCompiler]
  854. public static void double2_uniform()
  855. {
  856. var rnd = new Random(0x6E624EB7u);
  857. ks_test((() => range_check01(rnd.NextDouble2().x)));
  858. ks_test((() => range_check01(rnd.NextDouble2().y)));
  859. }
  860. [TestCompiler]
  861. public static void double2_uniform_low_bits()
  862. {
  863. var rnd = new Random(0x6E624EB7u);
  864. ks_test((() => frac(rnd.NextDouble2().x * 35184372088832.0)));
  865. ks_test((() => frac(rnd.NextDouble2().y * 35184372088832.0)));
  866. }
  867. [TestCompiler]
  868. public static void double2_uniform_max()
  869. {
  870. var rnd = new Random(0x6E624EB7u);
  871. double2 max = double2(16.4, 1001.33333333);
  872. ks_test((() => range_check(rnd.NextDouble2(max).x, 0.0, max.x) / max.x));
  873. ks_test((() => range_check(rnd.NextDouble2(max).y, 0.0, max.y) / max.y));
  874. }
  875. [TestCompiler]
  876. public static void double2_uniform_min_max()
  877. {
  878. var rnd = new Random(0x6E624EB7u);
  879. double2 min = double2(-3.1, 17.1);
  880. double2 max = double2(16.4, 1001.33333333);
  881. double2 range = max - min;
  882. ks_test((() => (range_check(rnd.NextDouble2(min, max).x, min.x, max.x) - min.x) / range.x));
  883. ks_test((() => (range_check(rnd.NextDouble2(min, max).y, min.y, max.y) - min.y) / range.y));
  884. }
  885. [TestCompiler]
  886. public static void double2_independent()
  887. {
  888. var rnd = new Random(0x6E624EB7u);
  889. r_test((() => rnd.NextDouble2()));
  890. }
  891. [TestCompiler]
  892. public static void double3_uniform()
  893. {
  894. var rnd = new Random(0x6E624EB7u);
  895. ks_test((() => range_check01(rnd.NextDouble3().x)));
  896. ks_test((() => range_check01(rnd.NextDouble3().y)));
  897. ks_test((() => range_check01(rnd.NextDouble3().z)));
  898. }
  899. [TestCompiler]
  900. public static void double3_uniform_low_bits()
  901. {
  902. var rnd = new Random(0x6E624EB7u);
  903. ks_test((() => frac(rnd.NextDouble3().x * 35184372088832.0)));
  904. ks_test((() => frac(rnd.NextDouble3().y * 35184372088832.0)));
  905. ks_test((() => frac(rnd.NextDouble3().z * 35184372088832.0)));
  906. }
  907. [TestCompiler]
  908. public static void double3_uniform_max()
  909. {
  910. var rnd = new Random(0x6E624EB7u);
  911. double3 max = double3(16.4, 1001.33333333, 3.121);
  912. ks_test((() => range_check(rnd.NextDouble3(max).x, 0.0, max.x) / max.x));
  913. ks_test((() => range_check(rnd.NextDouble3(max).y, 0.0, max.y) / max.y));
  914. ks_test((() => range_check(rnd.NextDouble3(max).z, 0.0, max.z) / max.z));
  915. }
  916. [TestCompiler]
  917. public static void double3_uniform_min_max()
  918. {
  919. var rnd = new Random(0x6E624EB7u);
  920. double3 min = double3(-3.1, 17.1, -0.3);
  921. double3 max = double3(16.4, 1001.33333333, 3.121);
  922. double3 range = max - min;
  923. ks_test((() => (range_check(rnd.NextDouble3(min, max).x, min.x, max.x) - min.x) / range.x));
  924. ks_test((() => (range_check(rnd.NextDouble3(min, max).y, min.y, max.y) - min.y) / range.y));
  925. ks_test((() => (range_check(rnd.NextDouble3(min, max).z, min.z, max.z) - min.z) / range.z));
  926. }
  927. [TestCompiler]
  928. public static void double3_independent()
  929. {
  930. var rnd = new Random(0x6E624EB7u);
  931. r_test((() => rnd.NextDouble3().xy));
  932. r_test((() => rnd.NextDouble3().xz));
  933. r_test((() => rnd.NextDouble3().yz));
  934. }
  935. [TestCompiler]
  936. public static void double4_uniform()
  937. {
  938. var rnd = new Random(0x6E624EB7u);
  939. ks_test((() => range_check01(rnd.NextDouble4().x)));
  940. ks_test((() => range_check01(rnd.NextDouble4().y)));
  941. ks_test((() => range_check01(rnd.NextDouble4().z)));
  942. ks_test((() => range_check01(rnd.NextDouble4().w)));
  943. }
  944. [TestCompiler]
  945. public static void double4_uniform_low_bits()
  946. {
  947. var rnd = new Random(0x6E624EB7u);
  948. ks_test((() => frac(rnd.NextDouble4().x * 35184372088832.0)));
  949. ks_test((() => frac(rnd.NextDouble4().y * 35184372088832.0)));
  950. ks_test((() => frac(rnd.NextDouble4().z * 35184372088832.0)));
  951. ks_test((() => frac(rnd.NextDouble4().w * 35184372088832.0)));
  952. }
  953. [TestCompiler]
  954. public static void double4_uniform_max()
  955. {
  956. var rnd = new Random(0x6E624EB7u);
  957. double4 max = double4(16.4f, 1001.33333333f, 3.121f, 1.3232e23f);
  958. ks_test((() => range_check(rnd.NextDouble4(max).x, 0.0, max.x) / max.x));
  959. ks_test((() => range_check(rnd.NextDouble4(max).y, 0.0, max.y) / max.y));
  960. ks_test((() => range_check(rnd.NextDouble4(max).z, 0.0, max.z) / max.z));
  961. ks_test((() => range_check(rnd.NextDouble4(max).w, 0.0, max.w) / max.w));
  962. }
  963. [TestCompiler]
  964. public static void double4_uniform_min_max()
  965. {
  966. var rnd = new Random(0x6E624EB7u);
  967. double4 min = double4(-3.1, 17.1, -0.3, -22.6);
  968. double4 max = double4(16.4, 1001.33333333, 3.121, 1.3232e23);
  969. double4 range = max - min;
  970. ks_test((() => (range_check(rnd.NextDouble4(min, max).x, min.x, max.x) - min.x) / range.x));
  971. ks_test((() => (range_check(rnd.NextDouble4(min, max).y, min.y, max.y) - min.y) / range.y));
  972. ks_test((() => (range_check(rnd.NextDouble4(min, max).z, min.z, max.z) - min.z) / range.z));
  973. ks_test((() => (range_check(rnd.NextDouble4(min, max).w, min.w, max.w) - min.w) / range.w));
  974. }
  975. [TestCompiler]
  976. public static void double4_independent()
  977. {
  978. var rnd = new Random(0x6E624EB7u);
  979. r_test((() => rnd.NextDouble4().xy));
  980. r_test((() => rnd.NextDouble4().xz));
  981. r_test((() => rnd.NextDouble4().xw));
  982. r_test((() => rnd.NextDouble4().yz));
  983. r_test((() => rnd.NextDouble4().yw));
  984. r_test((() => rnd.NextDouble4().zw));
  985. }
  986. [TestCompiler]
  987. public static void float2_direction()
  988. {
  989. var rnd = new Random(0x6E624EB7u);
  990. ks_test(() => {
  991. float2 dir = rnd.NextFloat2Direction();
  992. TestUtils.AreEqual(length(dir), 1.0f, 0.001f);
  993. return atan2(dir.x, dir.y) / (2.0f * PI) + 0.5f;
  994. });
  995. }
  996. [TestCompiler]
  997. public static void double2_direction()
  998. {
  999. var rnd = new Random(0x6E624EB7u);
  1000. ks_test(() => {
  1001. double2 dir = rnd.NextFloat2Direction();
  1002. TestUtils.AreEqual(length(dir), 1.0, 0.000001);
  1003. return atan2(dir.y, dir.x) / (2.0 * PI_DBL) + 0.5;
  1004. });
  1005. }
  1006. [TestCompiler]
  1007. public static void float3_direction()
  1008. {
  1009. var rnd = new Random(0x6E624EB7u);
  1010. ks_test(() =>
  1011. {
  1012. float3 dir = rnd.NextFloat3Direction();
  1013. float r = length(dir);
  1014. TestUtils.AreEqual(r, 1.0f, 0.001f);
  1015. float phi = atan2(dir.y, dir.x) / (2.0f * PI) + 0.5f;
  1016. float z = saturate(dir.z / r * 0.5f + 0.5f);
  1017. return double2(phi, z);
  1018. });
  1019. }
  1020. [TestCompiler]
  1021. public static void double3_direction()
  1022. {
  1023. var rnd = new Random(0x6E624EB7u);
  1024. ks_test(() =>
  1025. {
  1026. double3 dir = rnd.NextDouble3Direction();
  1027. double r = length(dir);
  1028. TestUtils.AreEqual(r, 1.0, 0.00001);
  1029. double phi = atan2(dir.y, dir.x) / (2.0 * PI_DBL) + 0.5;
  1030. double z = saturate(dir.z / r * 0.5 + 0.5);
  1031. return double2(phi, z);
  1032. });
  1033. }
  1034. [TestCompiler]
  1035. public static void quaternion_rotation()
  1036. {
  1037. var rnd = new Random(0x6E624EB7u);
  1038. ks_test(() =>
  1039. {
  1040. quaternion q = rnd.NextQuaternionRotation();
  1041. TestUtils.AreEqual(dot(q, q), 1.0, 0.00001f);
  1042. Assert.GreaterOrEqual(q.value.w, 0.0f);
  1043. float3 p = float3(1.0f, 2.0f, 3.0f);
  1044. float3 qp = mul(q, p);
  1045. TestUtils.AreEqual(length(qp), length(p), 0.0001f);
  1046. float r = length(qp);
  1047. double phi = atan2(qp.y, qp.x) / (2.0 * PI_DBL) + 0.5;
  1048. double z = saturate(qp.z / r * 0.5 + 0.5);
  1049. return double2(phi, z);
  1050. });
  1051. }
  1052. [TestCompiler]
  1053. public static void consecutive_seeds_r_test()
  1054. {
  1055. // Check that drawing 1 number from many consecutive seeds has no correlation.
  1056. for (uint i = 0; i < 128; ++i)
  1057. {
  1058. uint seed1 = i;
  1059. uint seed2 = i + 1;
  1060. r_test(() => new double2(Random.CreateFromIndex(seed1++).NextUInt(), Random.CreateFromIndex(seed2++).NextUInt()));
  1061. }
  1062. }
  1063. [TestCompiler]
  1064. public static void consecutive_seeds_r_test2()
  1065. {
  1066. // Check that drawing 1 number from many consecutive seeds has no correlation.
  1067. for (uint i = 0; i < 128; ++i)
  1068. {
  1069. uint seed1 = 0x6E624EB7u + i;
  1070. uint seed2 = 0x6E624EB8u + i;
  1071. r_test(() => new double2(Random.CreateFromIndex(seed1++).NextUInt(), Random.CreateFromIndex(seed2++).NextUInt()));
  1072. }
  1073. }
  1074. [TestCompiler]
  1075. public static void consecutive_seeds_ks_test()
  1076. {
  1077. // Check that drawing 1 number from many consecutive seeds matches our expected distribution.
  1078. uint seed = 0;
  1079. ks_test(() => Random.CreateFromIndex(seed++).NextDouble());
  1080. }
  1081. [TestCompiler]
  1082. public static void consecutive_seeds_ks_test2()
  1083. {
  1084. // Check that drawing 1 number from many consecutive seeds matches our expected distribution.
  1085. uint seed = 0x6E624EB7u;
  1086. ks_test(() => Random.CreateFromIndex(seed++).NextDouble());
  1087. }
  1088. [TestCompiler]
  1089. public static void similar_seeds()
  1090. {
  1091. // Check to see that two consecutive seeds have no correlation when drawing
  1092. // many numbers from them.
  1093. for (uint i = 1; i <= 1024; ++i)
  1094. {
  1095. var rnd1 = new Random(i);
  1096. var rnd2 = new Random(i + 1);
  1097. r_test(() => new double2(rnd1.NextUInt(), rnd2.NextUInt()));
  1098. }
  1099. }
  1100. [TestCompiler]
  1101. public static void similar_seeds2()
  1102. {
  1103. // Check to see that two consecutive seeds have no correlation when drawing
  1104. // many numbers from them.
  1105. for (uint i = 0; i < 1024; ++i)
  1106. {
  1107. var rnd1 = new Random(0x6E624EB7u + i);
  1108. var rnd2 = new Random(0x6E624EB8u + i);
  1109. r_test(() => new double2(rnd1.NextUInt(), rnd2.NextUInt()));
  1110. }
  1111. }
  1112. [TestCompiler]
  1113. public static void wang_hash()
  1114. {
  1115. TestUtils.AreEqual(3232319850u, Random.WangHash(0u));
  1116. TestUtils.AreEqual(663891101u, Random.WangHash(1u));
  1117. TestUtils.AreEqual(3329832309u, Random.WangHash(2u));
  1118. TestUtils.AreEqual(2278584254u, Random.WangHash(3u));
  1119. TestUtils.AreEqual(3427349084u, Random.WangHash(4u));
  1120. TestUtils.AreEqual(3322605197u, Random.WangHash(5u));
  1121. TestUtils.AreEqual(1902946834u, Random.WangHash(6u));
  1122. TestUtils.AreEqual(851741419u, Random.WangHash(7u));
  1123. TestUtils.AreEqual(0u, Random.WangHash(61u));
  1124. unchecked
  1125. {
  1126. // Random.CreateFromIndex() takes zero as a valid input and makes uint.MaxValue invalid
  1127. // so check that we can make uint.MaxValue hash to zero.
  1128. TestUtils.AreEqual(0u, Random.WangHash(uint.MaxValue + 62u));
  1129. }
  1130. }
  1131. }
  1132. }