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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use std::time::SystemTime;

pub trait RandBasic {
    type Style;
    type Seed;
    type Attachment;
    type Return;
    fn new(style: Self::Style) -> Self;
    fn srand(&mut self, seed: Self::Seed);
    fn lazy_srand(&mut self);
    fn get_rand(&mut self, attachment: Option<Self::Attachment>) -> Self::Return;
    fn lazy_rand(&mut self, min: i64, max: i64) -> i64;
    fn lazy_randf(&mut self, min: f64, max: f64) -> f64;
}


const PI: f64 = 3.141592654;
pub const RAND_MAX: f64 = 2147483647.0;

/// #Example
///
/// ```Rust
/// extern crate xiangyun;
///
/// use xiangyun::{Rand, Style};
/// use std::{thread, time};
///
/// fn main() {
///     let millis = time::Duration::from_millis(1000);
///     let mut foo = Rand::new(Style::Ryus);
///     foo.lazy_srand();
///     println!("Ryus: {}", foo.lazy_rand(1, 100));
///     thread::sleep(millis);
///     foo.lazy_srand();
///     println!("Ryus: {}", foo.lazy_rand(1, 100));
///     println!("-------------------------------------------------------");
///     let mut foo = Rand::new(Style::PMrand);
///     foo.lazy_srand();
///     println!("PMrand: {}", foo.lazy_rand(1, 100));
///     thread::sleep(millis);
///     foo.lazy_srand();
///     println!("PMrand: {}", foo.lazy_rand(1, 100));
/// }
/// ```
pub enum Style {
    PMrand,
    Gauss,
    BMrand,
    Marsaglia,
    Lazy,
    Crand,
    Ryus,
    Dalton,
}

pub struct Rand {
    style: Style,
    seed: i64,
    attachment: Vec<f64>,
}

impl RandBasic for Rand {
    type Style = Style;
    type Seed = i64;
    type Attachment = u64;
    type Return = f64;

    /// Is used to create a new random sequence core
    fn new(style: Style) -> Self {
        Rand {
            style: style,
            seed: 1,
            attachment: Vec::new(),
        }
    }

    /// Is used to generate a fixed sequence
    fn srand(&mut self, seed: i64) {
        self.seed = seed;
    }

    /// Is used to set random seed
    fn lazy_srand(&mut self) {
        let mut seed: i64 = time_get();
        if seed > 0 {
            match self.style {
                Style::Crand => self.seed = 1,
                _ => self.seed = seed,
            }
        } else {
            self.seed = 1;
        }
    }

    fn get_rand(&mut self, attachment: Option<u64>) -> f64 {
        let mut a = 25;
        match attachment {
            Some(e) => a = e,
            _ => {}
        }
        match self.style {
            Style::PMrand => {
                self.seed = pmrand(self.seed, a as i64);
                (self.seed as f64 / RAND_MAX).abs()
            }
            Style::Gauss => (Rand::gauss(&mut self.seed, a) / RAND_MAX).abs(),
            Style::BMrand => (Rand::bmrand(&mut self.seed, &mut self.attachment) / RAND_MAX).abs(),
            Style::Marsaglia => {
                (Rand::marsaglia(&mut self.seed, &mut self.attachment) / RAND_MAX).abs()
            }
            Style::Crand => (Rand::crand(&mut self.seed) as f64 / RAND_MAX).abs(),
            Style::Ryus => (Rand::ryus(&mut self.seed) as f64 / RAND_MAX).abs(),
            Style::Dalton => (Rand::dalton(&mut self.seed, a) / RAND_MAX).abs(),
            _ => {
                self.seed = Rand::lazy(self.seed);
                (self.seed as f64 / RAND_MAX).abs()
            }
        }
    }

    fn lazy_rand(&mut self, min: i64, max: i64) -> i64 {
        let gap = max - min + 1;
        match self.style {
            Style::PMrand => min + (gap as f64 * self.get_rand(Some(48271))) as i64,
            Style::Gauss | Style::Dalton => {
                min + (gap as f64 * self.get_rand(Some((gap as u64)))) as i64
            }
            _ => min + (gap as f64 * self.get_rand(None)) as i64,
        }
    }

    fn lazy_randf(&mut self, min: f64, max: f64) -> f64 {
        let gap = max - min + 1.0;
        match self.style {
            Style::PMrand => min + self.get_rand(Some(48271)) * gap,
            Style::Gauss | Style::Dalton => min + self.get_rand(Some((gap as u64))) * gap,
            _ => min + self.get_rand(None) * gap,
        }
    }
}

#[inline]
pub fn pmrand(seed: i64, a: i64) -> i64 {
    let m = RAND_MAX as i64;
    let q = m / a;
    let r = m % a;
    let hi = seed / q;
    let lo = seed % q;
    let test = a * lo - r * hi;
    if test > 0 { test } else { test + m }
}

#[inline]
fn basic(seed: &mut i64) -> i64 {
    *seed = pmrand(*seed, 48271);
    *seed
}

#[inline]
fn basicf(seed: &mut i64) -> f64 {
    basic(seed) as f64
}

impl Rand {
    fn gauss(seed: &mut i64, nsum: u64) -> f64 {
        let mut x = 0.0;
        for _ in 0..nsum {
            x += basicf(seed) / RAND_MAX;
        }
        x -= nsum as f64 / 2.0;
        x /= (nsum as f64 / 12.0).sqrt();
        x
    }

    fn bmrand(seed: &mut i64, attachment: &mut Vec<f64>) -> f64 {
        if attachment.len() == 0 {
            attachment.push(0.0);
            attachment.push(0.0);
            attachment.push(0.0);
        }
        attachment[2] = 1.0 - attachment[2];
        if attachment[2] != 0.0 {
            attachment[0] = (basicf(seed) + 1.0) / (RAND_MAX + 2.0);
            attachment[1] = basicf(seed) / (RAND_MAX + 1.0);
            (2.0 * PI * attachment[1]).sin() * (-2.0 * attachment[0].log(10.0)).sqrt()
        } else {
            (2.0 * PI * attachment[1]).cos() * (-2.0 * attachment[0].log(10.0)).sqrt()
        }
    }

    fn marsaglia(seed: &mut i64, attachment: &mut Vec<f64>) -> f64 {
        if attachment.len() == 0 {
            attachment.push(0.0);
            attachment.push(0.0);
            attachment.push(0.0);
            attachment.push(0.0);
        }
        attachment[3] = 1.0 - attachment[3];
        if attachment[3] != 0.0 {
            loop {
                let u1 = basicf(seed) / RAND_MAX;
                let u2 = basicf(seed) / RAND_MAX;
                attachment[0] = 2.0 * u1 - 1.0;
                attachment[1] = 2.0 * u2 - 1.0;
                attachment[2] = attachment[0] * attachment[0] + attachment[1] * attachment[1];
                if attachment[2] < 1.0 && attachment[2] != 0.0 {
                    break;
                }
            }
            attachment[0] * (-2.0 * attachment[2].log(10.0) / attachment[2])
        } else {
            attachment[1] * (-2.0 * attachment[2].log(10.0) / attachment[2])
        }
    }

    fn lazy(seed: i64) -> i64 {
        pmrand(seed, 16807)
    }

    fn crand(seed: &mut i64) -> i64 {
        *seed = *seed * 1103515245 + 12345;
        (*seed >> 16) & (RAND_MAX as i64)
    }

    fn ryus(seed: &mut i64) -> i64 {
        let i = basicf(seed);
        let j = basicf(seed);
        let k = (basicf(seed) / RAND_MAX).abs();
        (i * k + j * (1.0 - k)) as i64
    }

    fn dalton(seed: &mut i64, nsum: u64) -> f64 {
        let mut x = 0.0;
        for _ in 0..nsum {
            *seed = Rand::ryus(seed);
            x += *seed as f64 / RAND_MAX;
        }
        x -= nsum as f64 / 2.0;
        x /= (nsum as f64 / 12.0).sqrt();
        x
    }
}

/// A macro that generates random numbers
///
/// #Example
///
/// ```Rust
/// #[macro_use]
/// extern crate xiangyun;
/// use xiangyun::{Rand, Style};
/// fn main() {
///     println!("Style::Lazy");
///     let foo = rand!(2);
///     println!("{}, {}", foo[0], foo[1]);
///     println!("Style::PMrand");
///     let foo = rand!(Style::PMrand, 2);
///     println!("{}, {}", foo[0], foo[1]);
///     println!("Style::Gauss");
///     let foo = rand!(Style::Gauss, 2);
///     println!("{}, {}", foo[0], foo[1]);
/// }
/// ```
#[macro_export]
macro_rules! rand {
    () => {{
        let mut foo = Rand::new(Style::Lazy);
        foo.lazy_srand();
        foo.get_rand(None)
    }};
    ($num: expr) => {{
        let mut foo = Rand::new(Style::Lazy);
        let mut bar: Vec<f64> = Vec::new();
        foo.lazy_srand();
        for _ in 0..$num {
            bar.push(foo.get_rand(None));
        }
        bar
    }};
    ($style: path) => {{
        let mut foo = Rand::new($style);
        foo.lazy_srand();
        foo.get_rand(None)
    }};
    ($style: path, $num: expr) => {{
        let mut foo = Rand::new($style);
        let mut bar: Vec<f64> = Vec::new();
        foo.lazy_srand();
        for _ in 0..$num {
            bar.push(foo.get_rand(None));
        }
        bar
    }};
}

pub fn time_get() -> i64 {
    let sys_time = SystemTime::now();
    let foo_string = format!("{:?}", sys_time);
    let mut seed: i64 = 0;
    let mut flag = false;
    for num in foo_string.chars() {
        match num {
            e @ '0'...'9' => {
                flag = true;
                seed = seed * 10 + (e as u8 - 48) as i64;
                if seed >= i32::max_value() as i64 {
                    break;
                }
            }
            _ => {
                if flag {
                    break;
                }
            }
        }
    }
    seed
}