random.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using static Unity.Mathematics.math;
  4. using System.Diagnostics;
  5. namespace Unity.Mathematics
  6. {
  7. /// <summary>
  8. /// Random Number Generator based on xorshift.
  9. /// Designed for minimal state (32bits) to be easily embeddable into components.
  10. /// Core functionality is integer multiplication free to improve vectorization
  11. /// on less capable SIMD instruction sets.
  12. /// </summary>
  13. [Serializable]
  14. public partial struct Random
  15. {
  16. public uint state;
  17. /// <summary>
  18. /// Constructs a Random instance with a given seed value. The seed must be non-zero.
  19. /// </summary>
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. public Random(uint seed)
  22. {
  23. state = seed;
  24. CheckInitState();
  25. NextState();
  26. }
  27. /// <summary>
  28. /// Initialized the state of the Random instance with a given seed value. The seed must be non-zero.
  29. /// </summary>
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. public void InitState(uint seed = 0x6E624EB7u)
  32. {
  33. state = seed;
  34. NextState();
  35. }
  36. /// <summary>Returns a uniformly random bool value.</summary>
  37. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38. public bool NextBool()
  39. {
  40. return (NextState() & 1) == 1;
  41. }
  42. /// <summary>Returns a uniformly random bool2 value.</summary>
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public bool2 NextBool2()
  45. {
  46. uint v = NextState();
  47. return (uint2(v) & uint2(1, 2)) == 0;
  48. }
  49. /// <summary>Returns a uniformly random bool3 value.</summary>
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public bool3 NextBool3()
  52. {
  53. uint v = NextState();
  54. return (uint3(v) & uint3(1, 2, 4)) == 0;
  55. }
  56. /// <summary>Returns a uniformly random bool4 value.</summary>
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public bool4 NextBool4()
  59. {
  60. uint v = NextState();
  61. return (uint4(v) & uint4(1, 2, 4, 8)) == 0;
  62. }
  63. /// <summary>Returns a uniformly random int value in the interval [-2147483647, 2147483647].</summary>
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public int NextInt()
  66. {
  67. return (int)NextState() ^ -2147483648;
  68. }
  69. /// <summary>Returns a uniformly random int2 value with all components in the interval [-2147483647, 2147483647].</summary>
  70. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  71. public int2 NextInt2()
  72. {
  73. return int2((int)NextState(), (int)NextState()) ^ -2147483648;
  74. }
  75. /// <summary>Returns a uniformly random int3 value with all components in the interval [-2147483647, 2147483647].</summary>
  76. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  77. public int3 NextInt3()
  78. {
  79. return int3((int)NextState(), (int)NextState(), (int)NextState()) ^ -2147483648;
  80. }
  81. /// <summary>Returns a uniformly random int4 value with all components in the interval [-2147483647, 2147483647].</summary>
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public int4 NextInt4()
  84. {
  85. return int4((int)NextState(), (int)NextState(), (int)NextState(), (int)NextState()) ^ -2147483648;
  86. }
  87. /// <summary>Returns a uniformly random int value in the interval [0, max).</summary>
  88. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  89. public int NextInt(int max)
  90. {
  91. CheckNextIntMax(max);
  92. return (int)((NextState() * (ulong)max) >> 32);
  93. }
  94. /// <summary>Returns a uniformly random int2 value with all components in the interval [0, max).</summary>
  95. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  96. public int2 NextInt2(int2 max)
  97. {
  98. CheckNextIntMax(max.x);
  99. CheckNextIntMax(max.y);
  100. return int2((int)(NextState() * (ulong)max.x >> 32),
  101. (int)(NextState() * (ulong)max.y >> 32));
  102. }
  103. /// <summary>Returns a uniformly random int3 value with all components in the interval [0, max).</summary>
  104. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  105. public int3 NextInt3(int3 max)
  106. {
  107. CheckNextIntMax(max.x);
  108. CheckNextIntMax(max.y);
  109. CheckNextIntMax(max.z);
  110. return int3((int)(NextState() * (ulong)max.x >> 32),
  111. (int)(NextState() * (ulong)max.y >> 32),
  112. (int)(NextState() * (ulong)max.z >> 32));
  113. }
  114. /// <summary>Returns a uniformly random int4 value with all components in the interval [0, max).</summary>
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. public int4 NextInt4(int4 max)
  117. {
  118. CheckNextIntMax(max.x);
  119. CheckNextIntMax(max.y);
  120. CheckNextIntMax(max.z);
  121. CheckNextIntMax(max.w);
  122. return int4((int)(NextState() * (ulong)max.x >> 32),
  123. (int)(NextState() * (ulong)max.y >> 32),
  124. (int)(NextState() * (ulong)max.z >> 32),
  125. (int)(NextState() * (ulong)max.w >> 32));
  126. }
  127. /// <summary>Returns a uniformly random int value in the interval [min, max).</summary>
  128. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  129. public int NextInt(int min, int max)
  130. {
  131. CheckNextIntMinMax(min, max);
  132. uint range = (uint)(max - min);
  133. return (int)(NextState() * (ulong)range >> 32) + min;
  134. }
  135. /// <summary>Returns a uniformly random int2 value with all components in the interval [min, max).</summary>
  136. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  137. public int2 NextInt2(int2 min, int2 max)
  138. {
  139. CheckNextIntMinMax(min.x, max.x);
  140. CheckNextIntMinMax(min.y, max.y);
  141. uint2 range = (uint2)(max - min);
  142. return int2((int)(NextState() * (ulong)range.x >> 32),
  143. (int)(NextState() * (ulong)range.y >> 32)) + min;
  144. }
  145. /// <summary>Returns a uniformly random int3 value with all components in the interval [min, max).</summary>
  146. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  147. public int3 NextInt3(int3 min, int3 max)
  148. {
  149. CheckNextIntMinMax(min.x, max.x);
  150. CheckNextIntMinMax(min.y, max.y);
  151. CheckNextIntMinMax(min.z, max.z);
  152. uint3 range = (uint3)(max - min);
  153. return int3((int)(NextState() * (ulong)range.x >> 32),
  154. (int)(NextState() * (ulong)range.y >> 32),
  155. (int)(NextState() * (ulong)range.z >> 32)) + min;
  156. }
  157. /// <summary>Returns a uniformly random int4 value with all components in the interval [min, max).</summary>
  158. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  159. public int4 NextInt4(int4 min, int4 max)
  160. {
  161. CheckNextIntMinMax(min.x, max.x);
  162. CheckNextIntMinMax(min.y, max.y);
  163. CheckNextIntMinMax(min.z, max.z);
  164. CheckNextIntMinMax(min.w, max.w);
  165. uint4 range = (uint4)(max - min);
  166. return int4((int)(NextState() * (ulong)range.x >> 32),
  167. (int)(NextState() * (ulong)range.y >> 32),
  168. (int)(NextState() * (ulong)range.z >> 32),
  169. (int)(NextState() * (ulong)range.w >> 32)) + min;
  170. }
  171. /// <summary>Returns a uniformly random uint value in the interval [0, 4294967294].</summary>
  172. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  173. public uint NextUInt()
  174. {
  175. return NextState() - 1u;
  176. }
  177. /// <summary>Returns a uniformly random uint2 value with all components in the interval [0, 4294967294].</summary>
  178. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  179. public uint2 NextUInt2()
  180. {
  181. return uint2(NextState(), NextState()) - 1u;
  182. }
  183. /// <summary>Returns a uniformly random uint3 value with all components in the interval [0, 4294967294].</summary>
  184. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  185. public uint3 NextUInt3()
  186. {
  187. return uint3(NextState(), NextState(), NextState()) - 1u;
  188. }
  189. /// <summary>Returns a uniformly random uint4 value with all components in the interval [0, 4294967294].</summary>
  190. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  191. public uint4 NextUInt4()
  192. {
  193. return uint4(NextState(), NextState(), NextState(), NextState()) - 1u;
  194. }
  195. /// <summary>Returns a uniformly random uint value in the interval [0, max).</summary>
  196. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  197. public uint NextUInt(uint max)
  198. {
  199. return (uint)((NextState() * (ulong)max) >> 32);
  200. }
  201. /// <summary>Returns a uniformly random uint2 value with all components in the interval [0, max).</summary>
  202. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  203. public uint2 NextUInt2(uint2 max)
  204. {
  205. return uint2( (uint)(NextState() * (ulong)max.x >> 32),
  206. (uint)(NextState() * (ulong)max.y >> 32));
  207. }
  208. /// <summary>Returns a uniformly random uint3 value with all components in the interval [0, max).</summary>
  209. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  210. public uint3 NextUInt3(uint3 max)
  211. {
  212. return uint3( (uint)(NextState() * (ulong)max.x >> 32),
  213. (uint)(NextState() * (ulong)max.y >> 32),
  214. (uint)(NextState() * (ulong)max.z >> 32));
  215. }
  216. /// <summary>Returns a uniformly random uint4 value with all components in the interval [0, max).</summary>
  217. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  218. public uint4 NextUInt4(uint4 max)
  219. {
  220. return uint4( (uint)(NextState() * (ulong)max.x >> 32),
  221. (uint)(NextState() * (ulong)max.y >> 32),
  222. (uint)(NextState() * (ulong)max.z >> 32),
  223. (uint)(NextState() * (ulong)max.w >> 32));
  224. }
  225. /// <summary>Returns a uniformly random uint value in the interval [min, max).</summary>
  226. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  227. public uint NextUInt(uint min, uint max)
  228. {
  229. CheckNextUIntMinMax(min, max);
  230. uint range = max - min;
  231. return (uint)(NextState() * (ulong)range >> 32) + min;
  232. }
  233. /// <summary>Returns a uniformly random uint2 value with all components in the interval [min, max).</summary>
  234. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  235. public uint2 NextUInt2(uint2 min, uint2 max)
  236. {
  237. CheckNextUIntMinMax(min.x, max.x);
  238. CheckNextUIntMinMax(min.y, max.y);
  239. uint2 range = max - min;
  240. return uint2((uint)(NextState() * (ulong)range.x >> 32),
  241. (uint)(NextState() * (ulong)range.y >> 32)) + min;
  242. }
  243. /// <summary>Returns a uniformly random uint3 value with all components in the interval [min, max).</summary>
  244. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  245. public uint3 NextUInt3(uint3 min, uint3 max)
  246. {
  247. CheckNextUIntMinMax(min.x, max.x);
  248. CheckNextUIntMinMax(min.y, max.y);
  249. CheckNextUIntMinMax(min.z, max.z);
  250. uint3 range = max - min;
  251. return uint3((uint)(NextState() * (ulong)range.x >> 32),
  252. (uint)(NextState() * (ulong)range.y >> 32),
  253. (uint)(NextState() * (ulong)range.z >> 32)) + min;
  254. }
  255. /// <summary>Returns a uniformly random uint4 value with all components in the interval [min, max).</summary>
  256. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  257. public uint4 NextUInt4(uint4 min, uint4 max)
  258. {
  259. CheckNextUIntMinMax(min.x, max.x);
  260. CheckNextUIntMinMax(min.y, max.y);
  261. CheckNextUIntMinMax(min.z, max.z);
  262. CheckNextUIntMinMax(min.w, max.w);
  263. uint4 range = (uint4)(max - min);
  264. return uint4((uint)(NextState() * (ulong)range.x >> 32),
  265. (uint)(NextState() * (ulong)range.y >> 32),
  266. (uint)(NextState() * (ulong)range.z >> 32),
  267. (uint)(NextState() * (ulong)range.w >> 32)) + min;
  268. }
  269. /// <summary>Returns a uniformly random float value in the interval [0, 1).</summary>
  270. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  271. public float NextFloat()
  272. {
  273. return asfloat(0x3f800000 | (NextState() >> 9)) - 1.0f;
  274. }
  275. /// <summary>Returns a uniformly random float2 value with all components in the interval [0, 1).</summary>
  276. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  277. public float2 NextFloat2()
  278. {
  279. return asfloat(0x3f800000 | (uint2(NextState(), NextState()) >> 9)) - 1.0f;
  280. }
  281. /// <summary>Returns a uniformly random float3 value with all components in the interval [0, 1).</summary>
  282. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  283. public float3 NextFloat3()
  284. {
  285. return asfloat(0x3f800000 | (uint3(NextState(), NextState(), NextState()) >> 9)) - 1.0f;
  286. }
  287. /// <summary>Returns a uniformly random float4 value with all components in the interval [0, 1).</summary>
  288. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  289. public float4 NextFloat4()
  290. {
  291. return asfloat(0x3f800000 | (uint4(NextState(), NextState(), NextState(), NextState()) >> 9)) - 1.0f;
  292. }
  293. /// <summary>Returns a uniformly random float value in the interval [0, max).</summary>
  294. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  295. public float NextFloat(float max) { return NextFloat() * max; }
  296. /// <summary>Returns a uniformly random float2 value with all components in the interval [0, max).</summary>
  297. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  298. public float2 NextFloat2(float2 max) { return NextFloat2() * max; }
  299. /// <summary>Returns a uniformly random float3 value with all components in the interval [0, max).</summary>
  300. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  301. public float3 NextFloat3(float3 max) { return NextFloat3() * max; }
  302. /// <summary>Returns a uniformly random float4 value with all components in the interval [0, max).</summary>
  303. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  304. public float4 NextFloat4(float4 max) { return NextFloat4() * max; }
  305. /// <summary>Returns a uniformly random float value in the interval [min, max).</summary>
  306. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  307. public float NextFloat(float min, float max) { return NextFloat() * (max - min) + min; }
  308. /// <summary>Returns a uniformly random float2 value with all components in the interval [min, max).</summary>
  309. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  310. public float2 NextFloat2(float2 min, float2 max) { return NextFloat2() * (max - min) + min; }
  311. /// <summary>Returns a uniformly random float3 value with all components in the interval [min, max).</summary>
  312. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  313. public float3 NextFloat3(float3 min, float3 max) { return NextFloat3() * (max - min) + min; }
  314. /// <summary>Returns a uniformly random float4 value with all components in the interval [min, max).</summary>
  315. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  316. public float4 NextFloat4(float4 min, float4 max) { return NextFloat4() * (max - min) + min; }
  317. /// <summary>Returns a uniformly random double value in the interval [0, 1).</summary>
  318. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  319. public double NextDouble()
  320. {
  321. ulong sx = ((ulong)NextState() << 20) ^ NextState();
  322. return asdouble(0x3ff0000000000000 | sx) - 1.0;
  323. }
  324. /// <summary>Returns a uniformly random double2 value with all components in the interval [0, 1).</summary>
  325. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  326. public double2 NextDouble2()
  327. {
  328. ulong sx = ((ulong)NextState() << 20) ^ NextState();
  329. ulong sy = ((ulong)NextState() << 20) ^ NextState();
  330. return double2(asdouble(0x3ff0000000000000 | sx),
  331. asdouble(0x3ff0000000000000 | sy)) - 1.0;
  332. }
  333. /// <summary>Returns a uniformly random double3 value with all components in the interval [0, 1).</summary>
  334. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  335. public double3 NextDouble3()
  336. {
  337. ulong sx = ((ulong)NextState() << 20) ^ NextState();
  338. ulong sy = ((ulong)NextState() << 20) ^ NextState();
  339. ulong sz = ((ulong)NextState() << 20) ^ NextState();
  340. return double3(asdouble(0x3ff0000000000000 | sx),
  341. asdouble(0x3ff0000000000000 | sy),
  342. asdouble(0x3ff0000000000000 | sz)) - 1.0;
  343. }
  344. /// <summary>Returns a uniformly random double4 value with all components in the interval [0, 1).</summary>
  345. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  346. public double4 NextDouble4()
  347. {
  348. ulong sx = ((ulong)NextState() << 20) ^ NextState();
  349. ulong sy = ((ulong)NextState() << 20) ^ NextState();
  350. ulong sz = ((ulong)NextState() << 20) ^ NextState();
  351. ulong sw = ((ulong)NextState() << 20) ^ NextState();
  352. return double4(asdouble(0x3ff0000000000000 | sx),
  353. asdouble(0x3ff0000000000000 | sy),
  354. asdouble(0x3ff0000000000000 | sz),
  355. asdouble(0x3ff0000000000000 | sw)) - 1.0;
  356. }
  357. /// <summary>Returns a uniformly random double value in the interval [0, max).</summary>
  358. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  359. public double NextDouble(double max) { return NextDouble() * max; }
  360. /// <summary>Returns a uniformly random double2 value with all components in the interval [0, max).</summary>
  361. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  362. public double2 NextDouble2(double2 max) { return NextDouble2() * max; }
  363. /// <summary>Returns a uniformly random double3 value with all components in the interval [0, max).</summary>
  364. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  365. public double3 NextDouble3(double3 max) { return NextDouble3() * max; }
  366. /// <summary>Returns a uniformly random double4 value with all components in the interval [0, max).</summary>
  367. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  368. public double4 NextDouble4(double4 max) { return NextDouble4() * max; }
  369. /// <summary>Returns a uniformly random double value in the interval [min, max).</summary>
  370. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  371. public double NextDouble(double min, double max) { return NextDouble() * (max - min) + min; }
  372. /// <summary>Returns a uniformly random double2 value with all components in the interval [min, max).</summary>
  373. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  374. public double2 NextDouble2(double2 min, double2 max) { return NextDouble2() * (max - min) + min; }
  375. /// <summary>Returns a uniformly random double3 value with all components in the interval [min, max).</summary>
  376. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  377. public double3 NextDouble3(double3 min, double3 max) { return NextDouble3() * (max - min) + min; }
  378. /// <summary>Returns a uniformly random double4 value with all components in the interval [min, max).</summary>
  379. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  380. public double4 NextDouble4(double4 min, double4 max) { return NextDouble4() * (max - min) + min; }
  381. /// <summary>Returns a unit length float2 vector representing a uniformly random 2D direction.</summary>
  382. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  383. public float2 NextFloat2Direction()
  384. {
  385. float angle = NextFloat() * PI * 2.0f;
  386. float s, c;
  387. sincos(angle, out s, out c);
  388. return float2(c, s);
  389. }
  390. /// <summary>Returns a unit length double2 vector representing a uniformly random 2D direction.</summary>
  391. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  392. public double2 NextDouble2Direction()
  393. {
  394. double angle = NextDouble() * PI_DBL * 2.0;
  395. double s, c;
  396. sincos(angle, out s, out c);
  397. return double2(c, s);
  398. }
  399. /// <summary>Returns a unit length float3 vector representing a uniformly random 3D direction.</summary>
  400. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  401. public float3 NextFloat3Direction()
  402. {
  403. float2 rnd = NextFloat2();
  404. float z = rnd.x * 2.0f - 1.0f;
  405. float r = sqrt(max(1.0f - z * z, 0.0f));
  406. float angle = rnd.y * PI * 2.0f;
  407. float s, c;
  408. sincos(angle, out s, out c);
  409. return float3(c*r, s*r, z);
  410. }
  411. /// <summary>Returns a unit length double3 vector representing a uniformly random 3D direction.</summary>
  412. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  413. public double3 NextDouble3Direction()
  414. {
  415. double2 rnd = NextDouble2();
  416. double z = rnd.x * 2.0 - 1.0;
  417. double r = sqrt(max(1.0 - z * z, 0.0));
  418. double angle = rnd.y * PI_DBL * 2.0;
  419. double s, c;
  420. sincos(angle, out s, out c);
  421. return double3(c * r, s * r, z);
  422. }
  423. /// <summary>Returns a unit length quaternion representing a uniformly 3D rotation.</summary>
  424. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  425. public quaternion NextQuaternionRotation()
  426. {
  427. float3 rnd = NextFloat3(float3(2.0f * PI, 2.0f * PI, 1.0f));
  428. float u1 = rnd.z;
  429. float2 theta_rho = rnd.xy;
  430. float i = sqrt(1.0f - u1);
  431. float j = sqrt(u1);
  432. float2 sin_theta_rho;
  433. float2 cos_theta_rho;
  434. sincos(theta_rho, out sin_theta_rho, out cos_theta_rho);
  435. quaternion q = quaternion(i * sin_theta_rho.x, i * cos_theta_rho.x, j * sin_theta_rho.y, j * cos_theta_rho.y);
  436. return quaternion(select(q.value, -q.value, q.value.w < 0.0f));
  437. }
  438. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  439. private uint NextState()
  440. {
  441. CheckState();
  442. uint t = state;
  443. state ^= state << 13;
  444. state ^= state >> 17;
  445. state ^= state << 5;
  446. return t;
  447. }
  448. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  449. private void CheckInitState()
  450. {
  451. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  452. if (state == 0)
  453. throw new System.ArgumentException("Seed must be non-zero");
  454. #endif
  455. }
  456. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  457. private void CheckState()
  458. {
  459. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  460. if(state == 0)
  461. throw new System.ArgumentException("Invalid state 0. Random object has not been properly initialized");
  462. #endif
  463. }
  464. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  465. private void CheckNextIntMax(int max)
  466. {
  467. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  468. if (max < 0)
  469. throw new System.ArgumentException("max must be positive");
  470. #endif
  471. }
  472. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  473. private void CheckNextIntMinMax(int min, int max)
  474. {
  475. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  476. if (min > max)
  477. throw new System.ArgumentException("min must be less than or equal to max");
  478. #endif
  479. }
  480. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  481. private void CheckNextUIntMinMax(uint min, uint max)
  482. {
  483. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  484. if (min > max)
  485. throw new System.ArgumentException("min must be less than or equal to max");
  486. #endif
  487. }
  488. }
  489. }