1macro_rules! impl_vec_types {
2 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident) => {
3 impl Distribution<$vec2> for Standard {
4 #[inline]
5 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec2 {
6 rng.gen::<[$t; 2]>().into()
7 }
8 }
9
10 impl Distribution<$vec3> for Standard {
11 #[inline]
12 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec3 {
13 rng.gen::<[$t; 3]>().into()
14 }
15 }
16
17 impl Distribution<$vec4> for Standard {
18 #[inline]
19 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec4 {
20 rng.gen::<[$t; 4]>().into()
21 }
22 }
23
24 #[test]
25 fn test_vec2_rand() {
26 use rand::{Rng, SeedableRng};
27 use rand_xoshiro::Xoshiro256Plus;
28 let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
29 let a: ($t, $t) = rng1.gen();
30 let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
31 let b: $vec2 = rng2.gen();
32 assert_eq!(a, b.into());
33 }
34
35 #[test]
36 fn test_vec3_rand() {
37 use rand::{Rng, SeedableRng};
38 use rand_xoshiro::Xoshiro256Plus;
39 let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
40 let a: ($t, $t, $t) = rng1.gen();
41 let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
42 let b: $vec3 = rng2.gen();
43 assert_eq!(a, b.into());
44 }
45
46 #[test]
47 fn test_vec4_rand() {
48 use rand::{Rng, SeedableRng};
49 use rand_xoshiro::Xoshiro256Plus;
50 let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
51 let a: ($t, $t, $t, $t) = rng1.gen();
52 let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
53 let b: $vec4 = rng2.gen();
54 assert_eq!(a, b.into());
55 }
56 };
57}
58
59macro_rules! impl_float_types {
60 ($t:ident, $mat2:ident, $mat3:ident, $mat4:ident, $quat:ident, $vec2:ident, $vec3:ident, $vec4:ident) => {
61 impl_vec_types!($t, $vec2, $vec3, $vec4);
62
63 impl Distribution<$mat2> for Standard {
64 #[inline]
65 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat2 {
66 $mat2::from_cols_array(&rng.gen())
67 }
68 }
69
70 impl Distribution<$mat3> for Standard {
71 #[inline]
72 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat3 {
73 $mat3::from_cols_array(&rng.gen())
74 }
75 }
76
77 impl Distribution<$mat4> for Standard {
78 #[inline]
79 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat4 {
80 $mat4::from_cols_array(&rng.gen())
81 }
82 }
83
84 impl Distribution<$quat> for Standard {
85 #[inline]
86 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $quat {
87 let z = rng.gen_range::<$t, _>(-1.0..=1.0);
88 let (y, x) = math::sin_cos(rng.gen_range::<$t, _>(0.0..TAU));
89 let r = math::sqrt(1.0 - z * z);
90 let axis = $vec3::new(r * x, r * y, z);
91 let angle = rng.gen_range::<$t, _>(0.0..TAU);
92 $quat::from_axis_angle(axis, angle)
93 }
94 }
95
96 #[test]
97 fn test_mat2_rand() {
98 use rand::{Rng, SeedableRng};
99 use rand_xoshiro::Xoshiro256Plus;
100 let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
101 let a = $mat2::from_cols_array(&rng1.gen::<[$t; 4]>());
102 let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
103 let b = rng2.gen::<$mat2>();
104 assert_eq!(a, b);
105 }
106
107 #[test]
108 fn test_mat3_rand() {
109 use rand::{Rng, SeedableRng};
110 use rand_xoshiro::Xoshiro256Plus;
111 let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
112 let a = $mat3::from_cols_array(&rng1.gen::<[$t; 9]>());
113 let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
114 let b = rng2.gen::<$mat3>();
115 assert_eq!(a, b);
116 }
117
118 #[test]
119 fn test_mat4_rand() {
120 use rand::{Rng, SeedableRng};
121 use rand_xoshiro::Xoshiro256Plus;
122 let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
123 let a = $mat4::from_cols_array(&rng1.gen::<[$t; 16]>());
124 let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
125 let b = rng2.gen::<$mat4>();
126 assert_eq!(a, b);
127 }
128
129 #[test]
130 fn test_quat_rand() {
131 use rand::{Rng, SeedableRng};
132 use rand_xoshiro::Xoshiro256Plus;
133 let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
134 let a: $quat = rng1.gen();
135 assert!(a.is_normalized());
136 let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
137 let b: $quat = rng2.gen();
138 assert_eq!(a, b);
139 }
140 };
141}
142
143mod f32 {
144 use crate::f32::math;
145 use crate::{Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4};
146 use core::f32::consts::TAU;
147 use rand::{
148 distributions::{Distribution, Standard},
149 Rng,
150 };
151
152 impl_float_types!(f32, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4);
153
154 impl Distribution<Vec3A> for Standard {
155 #[inline]
156 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec3A {
157 rng.gen::<[f32; 3]>().into()
158 }
159 }
160
161 #[test]
162 fn test_vec3a_rand() {
163 use rand::{Rng, SeedableRng};
164 use rand_xoshiro::Xoshiro256Plus;
165 let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
166 let a: (f32, f32, f32) = rng1.gen();
167 let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
168 let b: Vec3A = rng2.gen();
169 assert_eq!(a, b.into());
170 }
171}
172
173mod f64 {
174 use crate::f64::math;
175 use crate::{DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4};
176 use core::f64::consts::TAU;
177 use rand::{
178 distributions::{Distribution, Standard},
179 Rng,
180 };
181
182 impl_float_types!(f64, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4);
183}
184
185mod i8 {
186 use crate::{I8Vec2, I8Vec3, I8Vec4};
187 use rand::{
188 distributions::{Distribution, Standard},
189 Rng,
190 };
191
192 impl_vec_types!(i8, I8Vec2, I8Vec3, I8Vec4);
193}
194
195mod i16 {
196 use crate::{I16Vec2, I16Vec3, I16Vec4};
197 use rand::{
198 distributions::{Distribution, Standard},
199 Rng,
200 };
201
202 impl_vec_types!(i16, I16Vec2, I16Vec3, I16Vec4);
203}
204
205mod i32 {
206 use crate::{IVec2, IVec3, IVec4};
207 use rand::{
208 distributions::{Distribution, Standard},
209 Rng,
210 };
211
212 impl_vec_types!(i32, IVec2, IVec3, IVec4);
213}
214
215mod i64 {
216 use crate::{I64Vec2, I64Vec3, I64Vec4};
217 use rand::{
218 distributions::{Distribution, Standard},
219 Rng,
220 };
221
222 impl_vec_types!(i64, I64Vec2, I64Vec3, I64Vec4);
223}
224
225mod u8 {
226 use crate::{U8Vec2, U8Vec3, U8Vec4};
227 use rand::{
228 distributions::{Distribution, Standard},
229 Rng,
230 };
231
232 impl_vec_types!(u8, U8Vec2, U8Vec3, U8Vec4);
233}
234
235mod u16 {
236 use crate::{U16Vec2, U16Vec3, U16Vec4};
237 use rand::{
238 distributions::{Distribution, Standard},
239 Rng,
240 };
241
242 impl_vec_types!(u16, U16Vec2, U16Vec3, U16Vec4);
243}
244
245mod u32 {
246 use crate::{UVec2, UVec3, UVec4};
247 use rand::{
248 distributions::{Distribution, Standard},
249 Rng,
250 };
251
252 impl_vec_types!(u32, UVec2, UVec3, UVec4);
253}
254
255mod u64 {
256 use crate::{U64Vec2, U64Vec3, U64Vec4};
257 use rand::{
258 distributions::{Distribution, Standard},
259 Rng,
260 };
261
262 impl_vec_types!(u64, U64Vec2, U64Vec3, U64Vec4);
263}