1 /*
2  * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
3  * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
4  */
5 
6 #ifndef BX_RNG_H_HEADER_GUARD
7 #define BX_RNG_H_HEADER_GUARD
8 
9 #include "bx.h"
10 #include "math.h"
11 #include "uint32_t.h"
12 
13 namespace bx
14 {
15 	/// George Marsaglia's MWC
16 	class RngMwc
17 	{
18 	public:
19 		///
20 		RngMwc(uint32_t _z = 12345, uint32_t _w = 65435);
21 
22 		///
23 		void reset(uint32_t _z = 12345, uint32_t _w = 65435);
24 
25 		///
26 		uint32_t gen();
27 
28 	private:
29 		uint32_t m_z;
30 		uint32_t m_w;
31 	};
32 
33 	/// George Marsaglia's SHR3
34 	class RngShr3
35 	{
36 	public:
37 		///
38 		RngShr3(uint32_t _jsr = 34221);
39 
40 		///
41 		void reset(uint32_t _jsr = 34221);
42 
43 		///
44 		uint32_t gen();
45 
46 	private:
47 		uint32_t m_jsr;
48 	};
49 
50 	/// Returns random number between 0.0f and 1.0f.
51 	template <typename Rng>
52 	float frnd(Rng* _rng);
53 
54 	/// Returns random number between -1.0f and 1.0f.
55 	template <typename Rng>
56 	float frndh(Rng* _rng);
57 
58 	/// Generate random point on unit circle.
59 	template <typename Rng>
60 	bx::Vec3 randUnitCircle(Rng* _rng);
61 
62 	/// Generate random point on unit sphere.
63 	template <typename Rng>
64 	bx::Vec3 randUnitSphere(Rng* _rng);
65 
66 	/// Generate random point on unit hemisphere.
67 	template <typename Ty>
68 	bx::Vec3 randUnitHemisphere(Ty* _rng, const bx::Vec3& _normal);
69 
70 	/// Sampling with Hammersley and Halton Points.
71 	void generateSphereHammersley(void* _data, uint32_t _stride, uint32_t _num, float _scale = 1.0f);
72 
73 	/// Fisher-Yates shuffle.
74 	template<typename Rng, typename Ty>
75 	void shuffle(Rng* _rng, Ty* _array, uint32_t _num);
76 
77 } // namespace bx
78 
79 #include "inline/rng.inl"
80 
81 #endif // BX_RNG_H_HEADER_GUARD
82