```1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
```
``````//! Rand implementations for complex numbers

use crate::Complex;
use num_traits::Num;
use rand::distributions::Standard;
use rand::prelude::*;

impl<T> Distribution<Complex<T>> for Standard
where
T: Num + Clone,
Standard: Distribution<T>,
{
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Complex<T> {
Complex::new(self.sample(rng), self.sample(rng))
}
}

/// A generic random value distribution for complex numbers.
#[derive(Clone, Copy, Debug)]
pub struct ComplexDistribution<Re, Im = Re> {
re: Re,
im: Im,
}

impl<Re, Im> ComplexDistribution<Re, Im> {
/// Creates a complex distribution from independent
/// distributions of the real and imaginary parts.
pub fn new(re: Re, im: Im) -> Self {
ComplexDistribution { re, im }
}
}

impl<T, Re, Im> Distribution<Complex<T>> for ComplexDistribution<Re, Im>
where
T: Num + Clone,
Re: Distribution<T>,
Im: Distribution<T>,
{
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Complex<T> {
Complex::new(self.re.sample(rng), self.im.sample(rng))
}
}

#[cfg(test)]
fn test_rng() -> impl RngCore {
/// Simple `Rng` for testing without additional dependencies
struct XorShiftStar {
a: u64,
}

impl RngCore for XorShiftStar {
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}

fn next_u64(&mut self) -> u64 {
// https://en.wikipedia.org/wiki/Xorshift#xorshift*
self.a ^= self.a >> 12;
self.a ^= self.a << 25;
self.a ^= self.a >> 27;
self.a.wrapping_mul(0x2545_F491_4F6C_DD1D)
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
for chunk in dest.chunks_mut(8) {
let bytes = self.next_u64().to_le_bytes();
let slice = &bytes[..chunk.len()];
chunk.copy_from_slice(slice)
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
Ok(self.fill_bytes(dest))
}
}

XorShiftStar {
a: 0x0123_4567_89AB_CDEF,
}
}

#[test]
fn standard_f64() {
let mut rng = test_rng();
for _ in 0..100 {
let c: Complex<f64> = rng.gen();
assert!(c.re >= 0.0 && c.re < 1.0);
assert!(c.im >= 0.0 && c.im < 1.0);
}
}

#[test]
fn generic_standard_f64() {
let mut rng = test_rng();
let dist = ComplexDistribution::new(Standard, Standard);
for _ in 0..100 {
let c: Complex<f64> = rng.sample(&dist);
assert!(c.re >= 0.0 && c.re < 1.0);
assert!(c.im >= 0.0 && c.im < 1.0);
}
}

#[test]
fn generic_uniform_f64() {
use rand::distributions::Uniform;

let mut rng = test_rng();
let re = Uniform::new(-100.0, 0.0);
let im = Uniform::new(0.0, 100.0);
let dist = ComplexDistribution::new(re, im);
for _ in 0..100 {
// no type annotation required, since `Uniform` only produces one type.
let c = rng.sample(&dist);
assert!(c.re >= -100.0 && c.re < 0.0);
assert!(c.im >= 0.0 && c.im < 100.0);
}
}

#[test]
fn generic_mixed_f64() {
use rand::distributions::Uniform;

let mut rng = test_rng();
let re = Uniform::new(-100.0, 0.0);
let dist = ComplexDistribution::new(re, Standard);
for _ in 0..100 {
// no type annotation required, since `Uniform` only produces one type.
let c = rng.sample(&dist);
assert!(c.re >= -100.0 && c.re < 0.0);
assert!(c.im >= 0.0 && c.im < 1.0);
}
}

#[test]
fn generic_uniform_i32() {
use rand::distributions::Uniform;

let mut rng = test_rng();
let re = Uniform::new(-100, 0);
let im = Uniform::new(0, 100);
let dist = ComplexDistribution::new(re, im);
for _ in 0..100 {
// no type annotation required, since `Uniform` only produces one type.
let c = rng.sample(&dist);
assert!(c.re >= -100 && c.re < 0);
assert!(c.im >= 0 && c.im < 100);
}
}
``````