설명 없음
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.

random.cs 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using static Unity.Mathematics.math;
  4. using System.Diagnostics;
  5. using Unity.IL2CPP.CompilerServices;
  6. namespace Unity.Mathematics
  7. {
  8. /// <summary>
  9. /// Random Number Generator based on xorshift.
  10. /// Designed for minimal state (32bits) to be easily embeddable into components.
  11. /// Core functionality is integer multiplication free to improve vectorization
  12. /// on less capable SIMD instruction sets.
  13. /// </summary>
  14. [Il2CppEagerStaticClassConstruction]
  15. [Serializable]
  16. public partial struct Random
  17. {
  18. /// <summary>
  19. /// The random number generator state. It should not be zero.
  20. /// </summary>
  21. public uint state;
  22. /// <summary>
  23. /// Constructs a Random instance with a given seed value. The seed must be non-zero.
  24. /// </summary>
  25. /// <param name="seed">The seed to initialize with.</param>
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. public Random(uint seed)
  28. {
  29. state = seed;
  30. CheckInitState();
  31. NextState();
  32. }
  33. /// <summary>
  34. /// Constructs a <see cref="Random"/> instance with an index that gets hashed. The index must not be uint.MaxValue.
  35. /// </summary>
  36. /// <remarks>
  37. /// Use this function when you expect to create several Random instances in a loop.
  38. /// </remarks>
  39. /// <example>
  40. /// <code>
  41. /// for (uint i = 0; i &lt; 4096; ++i)
  42. /// {
  43. /// Random rand = Random.CreateFromIndex(i);
  44. ///
  45. /// // Random numbers drawn from loop iteration j will be very different
  46. /// // from every other loop iteration k.
  47. /// rand.NextUInt();
  48. /// }
  49. /// </code>
  50. /// </example>
  51. /// <param name="index">An index that will be hashed for Random creation. Must not be uint.MaxValue.</param>
  52. /// <returns><see cref="Random"/> created from an index.</returns>
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public static Random CreateFromIndex(uint index)
  55. {
  56. CheckIndexForHash(index);
  57. // Wang hash will hash 61 to zero but we want uint.MaxValue to hash to zero. To make this happen
  58. // we must offset by 62.
  59. return new Random(WangHash(index + 62u));
  60. }
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. internal static uint WangHash(uint n)
  63. {
  64. // https://gist.github.com/badboy/6267743#hash-function-construction-principles
  65. // Wang hash: this has the property that none of the outputs will
  66. // collide with each other, which is important for the purposes of
  67. // seeding a random number generator. This was verified empirically
  68. // by checking all 2^32 uints.
  69. n = (n ^ 61u) ^ (n >> 16);
  70. n *= 9u;
  71. n = n ^ (n >> 4);
  72. n *= 0x27d4eb2du;
  73. n = n ^ (n >> 15);
  74. return n;
  75. }
  76. /// <summary>
  77. /// Initialized the state of the Random instance with a given seed value. The seed must be non-zero.
  78. /// </summary>
  79. /// <param name="seed">The seed to initialize with.</param>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public void InitState(uint seed = 0x6E624EB7u)
  82. {
  83. state = seed;
  84. NextState();
  85. }
  86. /// <summary>Returns a uniformly random bool value.</summary>
  87. /// <returns>A uniformly random boolean value.</returns>
  88. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  89. public bool NextBool()
  90. {
  91. return (NextState() & 1) == 1;
  92. }
  93. /// <summary>Returns a uniformly random bool2 value.</summary>
  94. /// <returns>A uniformly random bool2 value.</returns>
  95. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  96. public bool2 NextBool2()
  97. {
  98. uint v = NextState();
  99. return (uint2(v) & uint2(1, 2)) == 0;
  100. }
  101. /// <summary>Returns a uniformly random bool3 value.</summary>
  102. /// <returns>A uniformly random bool3 value.</returns>
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. public bool3 NextBool3()
  105. {
  106. uint v = NextState();
  107. return (uint3(v) & uint3(1, 2, 4)) == 0;
  108. }
  109. /// <summary>Returns a uniformly random bool4 value.</summary>
  110. /// <returns>A uniformly random bool4 value.</returns>
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. public bool4 NextBool4()
  113. {
  114. uint v = NextState();
  115. return (uint4(v) & uint4(1, 2, 4, 8)) == 0;
  116. }
  117. /// <summary>Returns a uniformly random int value in the interval [-2147483647, 2147483647].</summary>
  118. /// <returns>A uniformly random integer value.</returns>
  119. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  120. public int NextInt()
  121. {
  122. return (int)NextState() ^ -2147483648;
  123. }
  124. /// <summary>Returns a uniformly random int2 value with all components in the interval [-2147483647, 2147483647].</summary>
  125. /// <returns>A uniformly random int2 value.</returns>
  126. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  127. public int2 NextInt2()
  128. {
  129. return int2((int)NextState(), (int)NextState()) ^ -2147483648;
  130. }
  131. /// <summary>Returns a uniformly random int3 value with all components in the interval [-2147483647, 2147483647].</summary>
  132. /// <returns>A uniformly random int3 value.</returns>
  133. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  134. public int3 NextInt3()
  135. {
  136. return int3((int)NextState(), (int)NextState(), (int)NextState()) ^ -2147483648;
  137. }
  138. /// <summary>Returns a uniformly random int4 value with all components in the interval [-2147483647, 2147483647].</summary>
  139. /// <returns>A uniformly random int4 value.</returns>
  140. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  141. public int4 NextInt4()
  142. {
  143. return int4((int)NextState(), (int)NextState(), (int)NextState(), (int)NextState()) ^ -2147483648;
  144. }
  145. /// <summary>Returns a uniformly random int value in the interval [0, max).</summary>
  146. /// <param name="max">The maximum value to generate, exclusive.</param>
  147. /// <returns>A uniformly random int value in the range [0, max).</returns>
  148. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  149. public int NextInt(int max)
  150. {
  151. CheckNextIntMax(max);
  152. return (int)((NextState() * (ulong)max) >> 32);
  153. }
  154. /// <summary>Returns a uniformly random int2 value with all components in the interval [0, max).</summary>
  155. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  156. /// <returns>A uniformly random int2 value with all components in the range [0, max).</returns>
  157. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  158. public int2 NextInt2(int2 max)
  159. {
  160. CheckNextIntMax(max.x);
  161. CheckNextIntMax(max.y);
  162. return int2((int)(NextState() * (ulong)max.x >> 32),
  163. (int)(NextState() * (ulong)max.y >> 32));
  164. }
  165. /// <summary>Returns a uniformly random int3 value with all components in the interval [0, max).</summary>
  166. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  167. /// <returns>A uniformly random int3 value with all components in the range [0, max).</returns>
  168. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  169. public int3 NextInt3(int3 max)
  170. {
  171. CheckNextIntMax(max.x);
  172. CheckNextIntMax(max.y);
  173. CheckNextIntMax(max.z);
  174. return int3((int)(NextState() * (ulong)max.x >> 32),
  175. (int)(NextState() * (ulong)max.y >> 32),
  176. (int)(NextState() * (ulong)max.z >> 32));
  177. }
  178. /// <summary>Returns a uniformly random int4 value with all components in the interval [0, max).</summary>
  179. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  180. /// <returns>A uniformly random int4 value with all components in the range [0, max).</returns>
  181. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  182. public int4 NextInt4(int4 max)
  183. {
  184. CheckNextIntMax(max.x);
  185. CheckNextIntMax(max.y);
  186. CheckNextIntMax(max.z);
  187. CheckNextIntMax(max.w);
  188. return int4((int)(NextState() * (ulong)max.x >> 32),
  189. (int)(NextState() * (ulong)max.y >> 32),
  190. (int)(NextState() * (ulong)max.z >> 32),
  191. (int)(NextState() * (ulong)max.w >> 32));
  192. }
  193. /// <summary>Returns a uniformly random int value in the interval [min, max).</summary>
  194. /// <param name="min">The minimum value to generate, inclusive.</param>
  195. /// <param name="max">The maximum value to generate, exclusive.</param>
  196. /// <returns>A uniformly random integer between [min, max).</returns>
  197. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  198. public int NextInt(int min, int max)
  199. {
  200. CheckNextIntMinMax(min, max);
  201. uint range = (uint)(max - min);
  202. return (int)(NextState() * (ulong)range >> 32) + min;
  203. }
  204. /// <summary>Returns a uniformly random int2 value with all components in the interval [min, max).</summary>
  205. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  206. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  207. /// <returns>A uniformly random int2 between [min, max).</returns>
  208. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  209. public int2 NextInt2(int2 min, int2 max)
  210. {
  211. CheckNextIntMinMax(min.x, max.x);
  212. CheckNextIntMinMax(min.y, max.y);
  213. uint2 range = (uint2)(max - min);
  214. return int2((int)(NextState() * (ulong)range.x >> 32),
  215. (int)(NextState() * (ulong)range.y >> 32)) + min;
  216. }
  217. /// <summary>Returns a uniformly random int3 value with all components in the interval [min, max).</summary>
  218. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  219. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  220. /// <returns>A uniformly random int3 between [min, max).</returns>
  221. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  222. public int3 NextInt3(int3 min, int3 max)
  223. {
  224. CheckNextIntMinMax(min.x, max.x);
  225. CheckNextIntMinMax(min.y, max.y);
  226. CheckNextIntMinMax(min.z, max.z);
  227. uint3 range = (uint3)(max - min);
  228. return int3((int)(NextState() * (ulong)range.x >> 32),
  229. (int)(NextState() * (ulong)range.y >> 32),
  230. (int)(NextState() * (ulong)range.z >> 32)) + min;
  231. }
  232. /// <summary>Returns a uniformly random int4 value with all components in the interval [min, max).</summary>
  233. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  234. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  235. /// <returns>A uniformly random int4 between [min, max).</returns>
  236. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  237. public int4 NextInt4(int4 min, int4 max)
  238. {
  239. CheckNextIntMinMax(min.x, max.x);
  240. CheckNextIntMinMax(min.y, max.y);
  241. CheckNextIntMinMax(min.z, max.z);
  242. CheckNextIntMinMax(min.w, max.w);
  243. uint4 range = (uint4)(max - min);
  244. return int4((int)(NextState() * (ulong)range.x >> 32),
  245. (int)(NextState() * (ulong)range.y >> 32),
  246. (int)(NextState() * (ulong)range.z >> 32),
  247. (int)(NextState() * (ulong)range.w >> 32)) + min;
  248. }
  249. /// <summary>Returns a uniformly random uint value in the interval [0, 4294967294].</summary>
  250. /// <returns>A uniformly random unsigned integer.</returns>
  251. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  252. public uint NextUInt()
  253. {
  254. return NextState() - 1u;
  255. }
  256. /// <summary>Returns a uniformly random uint2 value with all components in the interval [0, 4294967294].</summary>
  257. /// <returns>A uniformly random uint2.</returns>
  258. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  259. public uint2 NextUInt2()
  260. {
  261. return uint2(NextState(), NextState()) - 1u;
  262. }
  263. /// <summary>Returns a uniformly random uint3 value with all components in the interval [0, 4294967294].</summary>
  264. /// <returns>A uniformly random uint3.</returns>
  265. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  266. public uint3 NextUInt3()
  267. {
  268. return uint3(NextState(), NextState(), NextState()) - 1u;
  269. }
  270. /// <summary>Returns a uniformly random uint4 value with all components in the interval [0, 4294967294].</summary>
  271. /// <returns>A uniformly random uint4.</returns>
  272. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  273. public uint4 NextUInt4()
  274. {
  275. return uint4(NextState(), NextState(), NextState(), NextState()) - 1u;
  276. }
  277. /// <summary>Returns a uniformly random uint value in the interval [0, max).</summary>
  278. /// <param name="max">The maximum value to generate, exclusive.</param>
  279. /// <returns>A uniformly random unsigned integer in the range [0, max).</returns>
  280. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  281. public uint NextUInt(uint max)
  282. {
  283. return (uint)((NextState() * (ulong)max) >> 32);
  284. }
  285. /// <summary>Returns a uniformly random uint2 value with all components in the interval [0, max).</summary>
  286. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  287. /// <returns>A uniformly random uint2 in the range [0, max).</returns>
  288. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  289. public uint2 NextUInt2(uint2 max)
  290. {
  291. return uint2( (uint)(NextState() * (ulong)max.x >> 32),
  292. (uint)(NextState() * (ulong)max.y >> 32));
  293. }
  294. /// <summary>Returns a uniformly random uint3 value with all components in the interval [0, max).</summary>
  295. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  296. /// <returns>A uniformly random uint3 in the range [0, max).</returns>
  297. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  298. public uint3 NextUInt3(uint3 max)
  299. {
  300. return uint3( (uint)(NextState() * (ulong)max.x >> 32),
  301. (uint)(NextState() * (ulong)max.y >> 32),
  302. (uint)(NextState() * (ulong)max.z >> 32));
  303. }
  304. /// <summary>Returns a uniformly random uint4 value with all components in the interval [0, max).</summary>
  305. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  306. /// <returns>A uniformly random uint4 in the range [0, max).</returns>
  307. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  308. public uint4 NextUInt4(uint4 max)
  309. {
  310. return uint4( (uint)(NextState() * (ulong)max.x >> 32),
  311. (uint)(NextState() * (ulong)max.y >> 32),
  312. (uint)(NextState() * (ulong)max.z >> 32),
  313. (uint)(NextState() * (ulong)max.w >> 32));
  314. }
  315. /// <summary>Returns a uniformly random uint value in the interval [min, max).</summary>
  316. /// <param name="min">The minimum value to generate, inclusive.</param>
  317. /// <param name="max">The maximum value to generate, exclusive.</param>
  318. /// <returns>A uniformly random unsigned integer in the range [min, max).</returns>
  319. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  320. public uint NextUInt(uint min, uint max)
  321. {
  322. CheckNextUIntMinMax(min, max);
  323. uint range = max - min;
  324. return (uint)(NextState() * (ulong)range >> 32) + min;
  325. }
  326. /// <summary>Returns a uniformly random uint2 value with all components in the interval [min, max).</summary>
  327. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  328. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  329. /// <returns>A uniformly random uint2 in the range [min, max).</returns>
  330. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  331. public uint2 NextUInt2(uint2 min, uint2 max)
  332. {
  333. CheckNextUIntMinMax(min.x, max.x);
  334. CheckNextUIntMinMax(min.y, max.y);
  335. uint2 range = max - min;
  336. return uint2((uint)(NextState() * (ulong)range.x >> 32),
  337. (uint)(NextState() * (ulong)range.y >> 32)) + min;
  338. }
  339. /// <summary>Returns a uniformly random uint3 value with all components in the interval [min, max).</summary>
  340. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  341. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  342. /// <returns>A uniformly random uint3 in the range [min, max).</returns>
  343. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  344. public uint3 NextUInt3(uint3 min, uint3 max)
  345. {
  346. CheckNextUIntMinMax(min.x, max.x);
  347. CheckNextUIntMinMax(min.y, max.y);
  348. CheckNextUIntMinMax(min.z, max.z);
  349. uint3 range = max - min;
  350. return uint3((uint)(NextState() * (ulong)range.x >> 32),
  351. (uint)(NextState() * (ulong)range.y >> 32),
  352. (uint)(NextState() * (ulong)range.z >> 32)) + min;
  353. }
  354. /// <summary>Returns a uniformly random uint4 value with all components in the interval [min, max).</summary>
  355. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  356. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  357. /// <returns>A uniformly random uint4 in the range [min, max).</returns>
  358. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  359. public uint4 NextUInt4(uint4 min, uint4 max)
  360. {
  361. CheckNextUIntMinMax(min.x, max.x);
  362. CheckNextUIntMinMax(min.y, max.y);
  363. CheckNextUIntMinMax(min.z, max.z);
  364. CheckNextUIntMinMax(min.w, max.w);
  365. uint4 range = (uint4)(max - min);
  366. return uint4((uint)(NextState() * (ulong)range.x >> 32),
  367. (uint)(NextState() * (ulong)range.y >> 32),
  368. (uint)(NextState() * (ulong)range.z >> 32),
  369. (uint)(NextState() * (ulong)range.w >> 32)) + min;
  370. }
  371. /// <summary>Returns a uniformly random float value in the interval [0, 1).</summary>
  372. /// <returns>A uniformly random float value in the range [0, 1).</returns>
  373. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  374. public float NextFloat()
  375. {
  376. return asfloat(0x3f800000 | (NextState() >> 9)) - 1.0f;
  377. }
  378. /// <summary>Returns a uniformly random float2 value with all components in the interval [0, 1).</summary>
  379. /// <returns>A uniformly random float2 value in the range [0, 1).</returns>
  380. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  381. public float2 NextFloat2()
  382. {
  383. return asfloat(0x3f800000 | (uint2(NextState(), NextState()) >> 9)) - 1.0f;
  384. }
  385. /// <summary>Returns a uniformly random float3 value with all components in the interval [0, 1).</summary>
  386. /// <returns>A uniformly random float3 value in the range [0, 1).</returns>
  387. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  388. public float3 NextFloat3()
  389. {
  390. return asfloat(0x3f800000 | (uint3(NextState(), NextState(), NextState()) >> 9)) - 1.0f;
  391. }
  392. /// <summary>Returns a uniformly random float4 value with all components in the interval [0, 1).</summary>
  393. /// <returns>A uniformly random float4 value in the range [0, 1).</returns>
  394. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  395. public float4 NextFloat4()
  396. {
  397. return asfloat(0x3f800000 | (uint4(NextState(), NextState(), NextState(), NextState()) >> 9)) - 1.0f;
  398. }
  399. /// <summary>Returns a uniformly random float value in the interval [0, max).</summary>
  400. /// <param name="max">The maximum value to generate, exclusive.</param>
  401. /// <returns>A uniformly random float value in the range [0, max).</returns>
  402. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  403. public float NextFloat(float max) { return NextFloat() * max; }
  404. /// <summary>Returns a uniformly random float2 value with all components in the interval [0, max).</summary>
  405. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  406. /// <returns>A uniformly random float2 value in the range [0, max).</returns>
  407. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  408. public float2 NextFloat2(float2 max) { return NextFloat2() * max; }
  409. /// <summary>Returns a uniformly random float3 value with all components in the interval [0, max).</summary>
  410. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  411. /// <returns>A uniformly random float3 value in the range [0, max).</returns>
  412. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  413. public float3 NextFloat3(float3 max) { return NextFloat3() * max; }
  414. /// <summary>Returns a uniformly random float4 value with all components in the interval [0, max).</summary>
  415. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  416. /// <returns>A uniformly random float4 value in the range [0, max).</returns>
  417. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  418. public float4 NextFloat4(float4 max) { return NextFloat4() * max; }
  419. /// <summary>Returns a uniformly random float value in the interval [min, max).</summary>
  420. /// <param name="min">The minimum value to generate, inclusive.</param>
  421. /// <param name="max">The maximum value to generate, exclusive.</param>
  422. /// <returns>A uniformly random float value in the range [min, max).</returns>
  423. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  424. public float NextFloat(float min, float max) { return NextFloat() * (max - min) + min; }
  425. /// <summary>Returns a uniformly random float2 value with all components in the interval [min, max).</summary>
  426. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  427. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  428. /// <returns>A uniformly random float2 value in the range [min, max).</returns>
  429. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  430. public float2 NextFloat2(float2 min, float2 max) { return NextFloat2() * (max - min) + min; }
  431. /// <summary>Returns a uniformly random float3 value with all components in the interval [min, max).</summary>
  432. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  433. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  434. /// <returns>A uniformly random float3 value in the range [min, max).</returns>
  435. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  436. public float3 NextFloat3(float3 min, float3 max) { return NextFloat3() * (max - min) + min; }
  437. /// <summary>Returns a uniformly random float4 value with all components in the interval [min, max).</summary>
  438. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  439. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  440. /// <returns>A uniformly random float4 value in the range [min, max).</returns>
  441. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  442. public float4 NextFloat4(float4 min, float4 max) { return NextFloat4() * (max - min) + min; }
  443. /// <summary>Returns a uniformly random double value in the interval [0, 1).</summary>
  444. /// <returns>A uniformly random double value in the range [0, 1).</returns>
  445. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  446. public double NextDouble()
  447. {
  448. ulong sx = ((ulong)NextState() << 20) ^ NextState();
  449. return asdouble(0x3ff0000000000000 | sx) - 1.0;
  450. }
  451. /// <summary>Returns a uniformly random double2 value with all components in the interval [0, 1).</summary>
  452. /// <returns>A uniformly random double2 value in the range [0, 1).</returns>
  453. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  454. public double2 NextDouble2()
  455. {
  456. ulong sx = ((ulong)NextState() << 20) ^ NextState();
  457. ulong sy = ((ulong)NextState() << 20) ^ NextState();
  458. return double2(asdouble(0x3ff0000000000000 | sx),
  459. asdouble(0x3ff0000000000000 | sy)) - 1.0;
  460. }
  461. /// <summary>Returns a uniformly random double3 value with all components in the interval [0, 1).</summary>
  462. /// <returns>A uniformly random double3 value in the range [0, 1).</returns>
  463. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  464. public double3 NextDouble3()
  465. {
  466. ulong sx = ((ulong)NextState() << 20) ^ NextState();
  467. ulong sy = ((ulong)NextState() << 20) ^ NextState();
  468. ulong sz = ((ulong)NextState() << 20) ^ NextState();
  469. return double3(asdouble(0x3ff0000000000000 | sx),
  470. asdouble(0x3ff0000000000000 | sy),
  471. asdouble(0x3ff0000000000000 | sz)) - 1.0;
  472. }
  473. /// <summary>Returns a uniformly random double4 value with all components in the interval [0, 1).</summary>
  474. /// <returns>A uniformly random double4 value in the range [0, 1).</returns>
  475. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  476. public double4 NextDouble4()
  477. {
  478. ulong sx = ((ulong)NextState() << 20) ^ NextState();
  479. ulong sy = ((ulong)NextState() << 20) ^ NextState();
  480. ulong sz = ((ulong)NextState() << 20) ^ NextState();
  481. ulong sw = ((ulong)NextState() << 20) ^ NextState();
  482. return double4(asdouble(0x3ff0000000000000 | sx),
  483. asdouble(0x3ff0000000000000 | sy),
  484. asdouble(0x3ff0000000000000 | sz),
  485. asdouble(0x3ff0000000000000 | sw)) - 1.0;
  486. }
  487. /// <summary>Returns a uniformly random double value in the interval [0, max).</summary>
  488. /// <param name="max">The maximum value to generate, exclusive.</param>
  489. /// <returns>A uniformly random double value in the range [0, max).</returns>
  490. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  491. public double NextDouble(double max) { return NextDouble() * max; }
  492. /// <summary>Returns a uniformly random double2 value with all components in the interval [0, max).</summary>
  493. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  494. /// <returns>A uniformly random double2 value in the range [0, max).</returns>
  495. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  496. public double2 NextDouble2(double2 max) { return NextDouble2() * max; }
  497. /// <summary>Returns a uniformly random double3 value with all components in the interval [0, max).</summary>
  498. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  499. /// <returns>A uniformly random double3 value in the range [0, max).</returns>
  500. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  501. public double3 NextDouble3(double3 max) { return NextDouble3() * max; }
  502. /// <summary>Returns a uniformly random double4 value with all components in the interval [0, max).</summary>
  503. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  504. /// <returns>A uniformly random double4 value in the range [0, max).</returns>
  505. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  506. public double4 NextDouble4(double4 max) { return NextDouble4() * max; }
  507. /// <summary>Returns a uniformly random double value in the interval [min, max).</summary>
  508. /// <param name="min">The minimum value to generate, inclusive.</param>
  509. /// <param name="max">The maximum value to generate, exclusive.</param>
  510. /// <returns>A uniformly random double value in the range [min, max).</returns>
  511. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  512. public double NextDouble(double min, double max) { return NextDouble() * (max - min) + min; }
  513. /// <summary>Returns a uniformly random double2 value with all components in the interval [min, max).</summary>
  514. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  515. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  516. /// <returns>A uniformly random double2 value in the range [min, max).</returns>
  517. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  518. public double2 NextDouble2(double2 min, double2 max) { return NextDouble2() * (max - min) + min; }
  519. /// <summary>Returns a uniformly random double3 value with all components in the interval [min, max).</summary>
  520. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  521. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  522. /// <returns>A uniformly random double3 value in the range [min, max).</returns>
  523. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  524. public double3 NextDouble3(double3 min, double3 max) { return NextDouble3() * (max - min) + min; }
  525. /// <summary>Returns a uniformly random double4 value with all components in the interval [min, max).</summary>
  526. /// <param name="min">The componentwise minimum value to generate, inclusive.</param>
  527. /// <param name="max">The componentwise maximum value to generate, exclusive.</param>
  528. /// <returns>A uniformly random double4 value in the range [min, max).</returns>
  529. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  530. public double4 NextDouble4(double4 min, double4 max) { return NextDouble4() * (max - min) + min; }
  531. /// <summary>Returns a unit length float2 vector representing a uniformly random 2D direction.</summary>
  532. /// <returns>A uniformly random unit length float2 vector.</returns>
  533. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  534. public float2 NextFloat2Direction()
  535. {
  536. float angle = NextFloat() * PI * 2.0f;
  537. float s, c;
  538. sincos(angle, out s, out c);
  539. return float2(c, s);
  540. }
  541. /// <summary>Returns a unit length double2 vector representing a uniformly random 2D direction.</summary>
  542. /// <returns>A uniformly random unit length double2 vector.</returns>
  543. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  544. public double2 NextDouble2Direction()
  545. {
  546. double angle = NextDouble() * PI_DBL * 2.0;
  547. double s, c;
  548. sincos(angle, out s, out c);
  549. return double2(c, s);
  550. }
  551. /// <summary>Returns a unit length float3 vector representing a uniformly random 3D direction.</summary>
  552. /// <returns>A uniformly random unit length float3 vector.</returns>
  553. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  554. public float3 NextFloat3Direction()
  555. {
  556. float2 rnd = NextFloat2();
  557. float z = rnd.x * 2.0f - 1.0f;
  558. float r = sqrt(max(1.0f - z * z, 0.0f));
  559. float angle = rnd.y * PI * 2.0f;
  560. float s, c;
  561. sincos(angle, out s, out c);
  562. return float3(c*r, s*r, z);
  563. }
  564. /// <summary>Returns a unit length double3 vector representing a uniformly random 3D direction.</summary>
  565. /// <returns>A uniformly random unit length double3 vector.</returns>
  566. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  567. public double3 NextDouble3Direction()
  568. {
  569. double2 rnd = NextDouble2();
  570. double z = rnd.x * 2.0 - 1.0;
  571. double r = sqrt(max(1.0 - z * z, 0.0));
  572. double angle = rnd.y * PI_DBL * 2.0;
  573. double s, c;
  574. sincos(angle, out s, out c);
  575. return double3(c * r, s * r, z);
  576. }
  577. /// <summary>Returns a unit length quaternion representing a uniformly 3D rotation.</summary>
  578. /// <returns>A uniformly random unit length quaternion.</returns>
  579. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  580. public quaternion NextQuaternionRotation()
  581. {
  582. float3 rnd = NextFloat3(float3(2.0f * PI, 2.0f * PI, 1.0f));
  583. float u1 = rnd.z;
  584. float2 theta_rho = rnd.xy;
  585. float i = sqrt(1.0f - u1);
  586. float j = sqrt(u1);
  587. float2 sin_theta_rho;
  588. float2 cos_theta_rho;
  589. sincos(theta_rho, out sin_theta_rho, out cos_theta_rho);
  590. quaternion q = quaternion(i * sin_theta_rho.x, i * cos_theta_rho.x, j * sin_theta_rho.y, j * cos_theta_rho.y);
  591. return quaternion(select(q.value, -q.value, q.value.w < 0.0f));
  592. }
  593. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  594. private uint NextState()
  595. {
  596. CheckState();
  597. uint t = state;
  598. state ^= state << 13;
  599. state ^= state >> 17;
  600. state ^= state << 5;
  601. return t;
  602. }
  603. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  604. private void CheckInitState()
  605. {
  606. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  607. if (state == 0)
  608. throw new System.ArgumentException("Seed must be non-zero");
  609. #endif
  610. }
  611. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  612. static void CheckIndexForHash(uint index)
  613. {
  614. if (index == uint.MaxValue)
  615. throw new System.ArgumentException("Index must not be uint.MaxValue");
  616. }
  617. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  618. private void CheckState()
  619. {
  620. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  621. if(state == 0)
  622. throw new System.ArgumentException("Invalid state 0. Random object has not been properly initialized");
  623. #endif
  624. }
  625. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  626. private void CheckNextIntMax(int max)
  627. {
  628. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  629. if (max < 0)
  630. throw new System.ArgumentException("max must be positive");
  631. #endif
  632. }
  633. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  634. private void CheckNextIntMinMax(int min, int max)
  635. {
  636. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  637. if (min > max)
  638. throw new System.ArgumentException("min must be less than or equal to max");
  639. #endif
  640. }
  641. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  642. private void CheckNextUIntMinMax(uint min, uint max)
  643. {
  644. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  645. if (min > max)
  646. throw new System.ArgumentException("min must be less than or equal to max");
  647. #endif
  648. }
  649. }
  650. }