diff --git a/Cargo.toml b/Cargo.toml index ef558df..bcf0223 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tinymt" -version = "1.0.7" +version = "1.0.8" authors = ["Torao Takami "] edition = "2021" repository = "https://github.com/torao/tinymt" @@ -9,6 +9,7 @@ license = "MIT" readme = "README.md" description = "Rust implementation of TinyMT 64/32 - a lightweight variant of Mersenne Twister PRNG" documentation = "https://docs.rs/tinymt" +members = ["cli"] [badges] circle-ci = { repository = "torao/tinymt", branch = "master" } @@ -23,4 +24,4 @@ rand = { version = "0.8", default-features = false } rand = { version = "0.8", default-features = false, features = ["getrandom"] } [target.'cfg(not(windows))'.dev-dependencies] -pprof = { version = "0.10", features = ["flamegraph"] } +pprof = { version = "0.11", features = ["flamegraph"] } diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 8fa8c93..86c8292 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -18,7 +18,7 @@ maintenance = { status = "passively-maintained" } [dependencies] tinymt = { version = "^1.0.0", path = "../" } -clap = "^3.2.21" +clap = "^4.1.8" rand = "^0.8.5" [[bin]] diff --git a/cli/src/main.rs b/cli/src/main.rs index 2fded57..35b162b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,8 +1,4 @@ -extern crate clap; -extern crate rand; -extern crate tinymt; - -use clap::{App, Arg}; +use clap::{Arg, Command}; use rand::{Rng, RngCore, SeedableRng}; use std::process; use std::str::FromStr; @@ -17,7 +13,7 @@ where } /// Generate TinyMT32 with specified seed. -fn rand32(seed: Option<&str>) -> Result { +fn rand32(seed: Option<&String>) -> Result { Ok(if let Some(seed) = seed { let seed: u32 = num(seed)?; TinyMT32::from_seed_u32(seed) @@ -27,7 +23,7 @@ fn rand32(seed: Option<&str>) -> Result { } /// Generate TinyMT64 with specified seed. -fn rand64(seed: Option<&str>) -> Result { +fn rand64(seed: Option<&String>) -> Result { Ok(if let Some(seed) = seed { let seed: u64 = num(seed)?; TinyMT64::from_seed_u64(seed) @@ -45,9 +41,9 @@ fn main() { fn exec() -> Result<(), String> { let args = arguments().get_matches(); - let seed = args.value_of("seed"); - let n: usize = num(args.value_of("count").unwrap())?; - match args.value_of("type").unwrap() { + let seed = args.get_one::("seed"); + let n = num(args.get_one::("count").unwrap())?; + match args.get_one::("type").unwrap().as_str() { "int" | "int32" | "i32" | "u32" | "32" => { print(&mut rand32(seed)?, n, |r| format!("{}", r.next_u32())) } @@ -62,8 +58,8 @@ fn exec() -> Result<(), String> { } "string" | "str" | "s" => { let mut r = rand64(seed)?; - let length: usize = num(args.value_of("length").unwrap())?; - let radix: Vec = args.value_of("radix").unwrap().chars().collect(); + let length: usize = num(args.get_one::("length").unwrap())?; + let radix: Vec = args.get_one::("radix").unwrap().chars().collect(); for _ in 0..n { println!( "{}", @@ -87,41 +83,41 @@ fn error(message: String) -> ! { process::exit(1) } -fn arguments() -> App<'static> { - App::new("tinymt") +fn arguments() -> Command { + Command::new("tinymt") .version("1.0") .author("Torao Takami ") .about("TinyMT 64/32-bit Random Number Generator CLI") .arg( - Arg::with_name("count") + Arg::new("count") .short('n') - .takes_value(true) + .number_of_values(1) .default_value("1") .help("number to generate"), ) .arg( - Arg::with_name("type") + Arg::new("type") .short('t') .long("type") - .takes_value(true) + .number_of_values(1) .default_value("double") .help("generate random number of int, long, float, double or string type"), ) .arg( - Arg::with_name("length") + Arg::new("length") .short('l') .long("length") - .takes_value(true) + .number_of_values(1) .default_value("32") .help("the number of characters when generating random strings"), ) .arg( - Arg::with_name("radix") + Arg::new("radix") .short('r') .long("radix") - .takes_value(true) + .number_of_values(1) .default_value("0123456789abcdefghijklmnopqrstuvwxyz") .help("characters to be used when generating random string"), ) - .arg(Arg::with_name("seed").help("seed of pseudo-random number generator")) + .arg(Arg::new("seed").help("seed of pseudo-random number generator")) } diff --git a/src/tinymt64.rs b/src/tinymt64.rs index 6e0debf..7da5da4 100644 --- a/src/tinymt64.rs +++ b/src/tinymt64.rs @@ -41,7 +41,7 @@ fn period_certification(random: &mut TinyMT64) { /// @param seed a 64-bit unsigned integer used as a seed. pub fn tinymt64_init(random: &mut TinyMT64, seed: u64) { random.status[0] = seed ^ ((random.mat1 as u64) << 32); - random.status[1] = (random.mat2 as u64) ^ (random.tmat as u64); + random.status[1] = (random.mat2 as u64) ^ random.tmat; for i in 1..MIN_LOOP { random.status[i & 1] ^= (i as u64).wrapping_add( 6_364_136_223_846_793_005_u64