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
//! Nonuniform pseudorandom number generation
//!
//! This library provides a suite of nonuniform random number generators
//! via bindings to the Numpy fork of RandomKit. It is approximately
//! equivalent to Numpy's `numpy.random` module. The API is loosely
//! based on that of the [`rand`](https://github.com/rust-lang/rand)
//! crate.
//!
//! This library is not suitable for cryptography.
//!
//! # Usage
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! randomkit = "0.1"
//! ```
//!
//! and this to your crate root:
//!
//! ```rust
//! extern crate randomkit;
//! ```
//!
//! # Examples
//!
//! ## Standard normal distribution
//!
//! Sample 1000 numbers from the standard normal distribution (Gauss
//! distribution) with mean 0 and standard deviation 1.
//!
//! ```rust
//! extern crate randomkit;
//!
//! use randomkit::{Rng, Sample};
//! use randomkit::dist::Gauss;
//!
//! fn main() {
//!     let mut rng = Rng::new().unwrap();
//!     for _ in 0..1000 {
//!         println!("{}", Gauss.sample(&mut rng));
//!     }
//! }
//! ```
//!
//! ## Normal distribution
//!
//! Sample 1000 numbers from a normal distribution with mean 10 and
//! standard deviation 5.
//!
//! ```rust
//! extern crate randomkit;
//!
//! use randomkit::{Rng, Sample};
//! use randomkit::dist::Normal;
//!
//! fn main() {
//!     let mut rng = Rng::from_seed(1);
//!     let normal = Normal::new(10.0, 5.0).unwrap();
//!     for _ in 0..1000 {
//!         println!("{}", normal.sample(&mut rng));
//!     }
//! }
//! ```

#![crate_name = "randomkit"]

extern crate libc;

use std::mem;
use libc::c_ulong;

pub mod dist;
mod ffi;

pub struct Rng { state: ffi::RkState }

impl Rng {
    unsafe fn uninitialized() -> Rng {
        Rng { state: mem::uninitialized() }
    }

    /// Initialize a new pseudorandom number generator from a seed.
    pub fn from_seed(seed: u32) -> Rng {
        // Seed is &'d with 0xffffffff in randomkit.c, so there's no
        // point in making it larger.
        unsafe {
            let mut r = Rng::uninitialized();
            ffi::rk_seed(seed as c_ulong, &mut r.state);
            r
        }
    }

    /// Initialize a new pseudorandom number generator using the
    /// operating system's random number generator as the seed.
    pub fn new() -> Option<Rng> {
        unsafe {
            let mut r = Rng::uninitialized();
            match ffi::rk_randomseed(&mut r.state) {
                ffi::RkError::RkNoerr => Some(r),
                _ => None,
            }
        }
    }
}

pub trait Sample<Support> {
    /// Generate a pseudorandom element of `Support` using `rng` as the
    /// source of randomness.
    fn sample(&self, rng: &mut Rng) -> Support;
}