gmp/
rand.rs

1use libc::{c_int, c_ulong, c_void};
2use super::mpz::{mpz_struct, Mpz, mpz_ptr, mpz_srcptr, mp_bitcnt_t};
3use std::mem::uninitialized;
4
5#[repr(C)]
6pub struct gmp_randstate_struct {
7    _mp_seed: mpz_struct,
8    _mp_alg: c_int,
9    _mp_algdata: *const c_void
10}
11
12pub type gmp_randstate_t = *mut gmp_randstate_struct;
13
14#[link(name = "gmp")]
15extern "C" {
16    fn __gmp_randinit_default(state: gmp_randstate_t);
17    fn __gmp_randinit_mt(state: gmp_randstate_t);
18    fn __gmp_randinit_lc_2exp(state: gmp_randstate_t, a: mpz_srcptr, c: c_ulong, m2exp: mp_bitcnt_t);
19    fn __gmp_randinit_lc_2exp_size(state: gmp_randstate_t, size: mp_bitcnt_t);
20    fn __gmp_randinit_set(state: gmp_randstate_t, op: *const gmp_randstate_struct);
21    fn __gmp_randclear(state: gmp_randstate_t);
22    fn __gmp_randseed(state: gmp_randstate_t, seed: mpz_srcptr);
23    fn __gmp_randseed_ui(state: gmp_randstate_t, seed: c_ulong);
24    fn __gmpz_urandomb(rop: mpz_ptr, state: gmp_randstate_t, n: mp_bitcnt_t);
25    fn __gmpz_urandomm(rop: mpz_ptr, state: gmp_randstate_t, n: mpz_srcptr);
26}
27
28pub struct RandState {
29    state: gmp_randstate_struct,
30}
31
32unsafe impl Send for RandState { }
33unsafe impl Sync for RandState { }
34
35impl Drop for RandState {
36    fn drop(&mut self) {
37        unsafe { __gmp_randclear(&mut self.state) }
38    }
39}
40
41impl RandState {
42    pub fn new() -> RandState {
43        unsafe {
44            let mut state: gmp_randstate_struct = uninitialized();
45            __gmp_randinit_default(&mut state);
46            RandState { state: state }
47        }
48    }
49
50    pub fn new_mt() -> RandState {
51        unsafe {
52            let mut state: gmp_randstate_struct = uninitialized();
53            __gmp_randinit_mt(&mut state);
54            RandState { state: state }
55        }
56    }
57
58    pub fn new_lc_2exp(a: Mpz, c: u64, m2exp: u64) -> RandState {
59        unsafe {
60            let mut state: gmp_randstate_struct = uninitialized();
61            __gmp_randinit_lc_2exp(&mut state, a.inner(), c as c_ulong, m2exp as c_ulong);
62            RandState { state: state }
63        }
64    }
65
66    pub fn new_lc_2exp_size(size: u64) -> RandState {
67        unsafe {
68            let mut state: gmp_randstate_struct = uninitialized();
69            __gmp_randinit_lc_2exp_size(&mut state, size as c_ulong);
70            RandState { state: state }
71        }
72    }
73
74    pub fn seed(&mut self, seed: Mpz) {
75        unsafe { __gmp_randseed(&mut self.state, seed.inner()) }
76    }
77
78    pub fn seed_ui(&mut self, seed: u64) {
79        unsafe { __gmp_randseed_ui(&mut self.state, seed as c_ulong) }
80    }
81
82    /// Generate a uniform random integer in the range 0 to n-1, inclusive
83    pub fn urandom(&mut self, n: &Mpz) -> Mpz {
84        unsafe {
85            let mut res = Mpz::new();
86            __gmpz_urandomm(res.inner_mut(), &mut self.state, n.inner());
87            res
88        }
89    }
90
91    /// Generate a uniformly distributed random integer in the range 0 to 2^n−1, inclusive.
92    pub fn urandom_2exp(&mut self, n: u64) -> Mpz {
93        unsafe {
94            let mut res = Mpz::new();
95            __gmpz_urandomb(res.inner_mut(), &mut self.state, n as c_ulong);
96            res
97        }
98    }
99}
100
101impl Clone for RandState {
102    fn clone(&self) -> RandState {
103        unsafe {
104            let mut state: gmp_randstate_struct = uninitialized();
105            __gmp_randinit_set(&mut state, &self.state);
106            RandState { state: state }
107        }
108    }
109}