r2rs-nmath 0.1.1

Statistics programming for Rust based on R's nmath package
Documentation
// Translation of nmath's rsignrank
//
// Copyright (C) 2020 neek-sss
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strafe_type::{FloatConstraint, Natural64, Real64};

use crate::traits::RNG;

/// Random variates from the Wilcoxon Signed Rank distribution.
pub fn rsignrank<N: Into<Natural64>, R: RNG>(n: N, rng: &mut R) -> Real64 {
    let mut n = n.into().unwrap();

    let mut i = 0;
    let mut k = 0;
    let mut r = 0.0;

    n = n.round();

    r = 0.0;
    k = n as i32;
    i = 0;
    while i < k {
        i += 1;
        r += i as f64 * (rng.unif_rand() + 0.5).floor()
    }
    r.into()
}