forked from kimhyunkang/rust-gmp
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrand.rs
109 lines (94 loc) · 3.33 KB
/
rand.rs
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
use libc::{c_int, c_ulong, c_void};
use super::mpz::{mpz_struct, Mpz, mpz_ptr, mpz_srcptr, mp_bitcnt_t};
use std::mem::uninitialized;
#[repr(C)]
pub struct gmp_randstate_struct {
_mp_seed: mpz_struct,
_mp_alg: c_int,
_mp_algdata: *const c_void
}
pub type gmp_randstate_t = *mut gmp_randstate_struct;
#[link(name = "gmp")]
extern "C" {
fn __gmp_randinit_default(state: gmp_randstate_t);
fn __gmp_randinit_mt(state: gmp_randstate_t);
fn __gmp_randinit_lc_2exp(state: gmp_randstate_t, a: mpz_srcptr, c: c_ulong, m2exp: mp_bitcnt_t);
fn __gmp_randinit_lc_2exp_size(state: gmp_randstate_t, size: mp_bitcnt_t);
fn __gmp_randinit_set(state: gmp_randstate_t, op: *const gmp_randstate_struct);
fn __gmp_randclear(state: gmp_randstate_t);
fn __gmp_randseed(state: gmp_randstate_t, seed: mpz_srcptr);
fn __gmp_randseed_ui(state: gmp_randstate_t, seed: c_ulong);
fn __gmpz_urandomb(rop: mpz_ptr, state: gmp_randstate_t, n: mp_bitcnt_t);
fn __gmpz_urandomm(rop: mpz_ptr, state: gmp_randstate_t, n: mpz_srcptr);
}
pub struct RandState {
state: gmp_randstate_struct,
}
unsafe impl Send for RandState { }
unsafe impl Sync for RandState { }
impl Drop for RandState {
fn drop(&mut self) {
unsafe { __gmp_randclear(&mut self.state) }
}
}
impl RandState {
pub fn new() -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
__gmp_randinit_default(&mut state);
RandState { state: state }
}
}
pub fn new_mt() -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
__gmp_randinit_mt(&mut state);
RandState { state: state }
}
}
pub fn new_lc_2exp(a: Mpz, c: u64, m2exp: u64) -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
__gmp_randinit_lc_2exp(&mut state, a.inner(), c as c_ulong, m2exp as c_ulong);
RandState { state: state }
}
}
pub fn new_lc_2exp_size(size: u64) -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
__gmp_randinit_lc_2exp_size(&mut state, size as c_ulong);
RandState { state: state }
}
}
pub fn seed(&mut self, seed: Mpz) {
unsafe { __gmp_randseed(&mut self.state, seed.inner()) }
}
pub fn seed_ui(&mut self, seed: u64) {
unsafe { __gmp_randseed_ui(&mut self.state, seed as c_ulong) }
}
/// Generate a uniform random integer in the range 0 to n-1, inclusive
pub fn urandom(&mut self, n: &Mpz) -> Mpz {
unsafe {
let mut res = Mpz::new();
__gmpz_urandomm(res.inner_mut(), &mut self.state, n.inner());
res
}
}
/// Generate a uniformly distributed random integer in the range 0 to 2^n−1, inclusive.
pub fn urandom_2exp(&mut self, n: u64) -> Mpz {
unsafe {
let mut res = Mpz::new();
__gmpz_urandomb(res.inner_mut(), &mut self.state, n as c_ulong);
res
}
}
}
impl Clone for RandState {
fn clone(&self) -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
__gmp_randinit_set(&mut state, &self.state);
RandState { state: state }
}
}
}