Initial commit

master
Avril 5 years ago
commit 3c05f5f26b
Signed by: flanchan
GPG Key ID: 284488987C31F630

3
.gitignore vendored

@ -0,0 +1,3 @@
/target
Cargo.lock
*~

@ -0,0 +1,10 @@
[package]
name = "rng"
version = "0.1.0"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
getrandom = "0.1"

@ -0,0 +1,51 @@
use super::*;
use getrandom::*;
pub fn range(start: f64, end: f64, mut repeat: usize) -> Result<(), Error>
{
if repeat < 1 {
repeat = 1;
}
for _ in 0..repeat {
let value = double()?;
println!("{}", start + (value * (end-start)));
}
Ok(())
}
pub fn round(start: f64, end: f64, mut repeat: usize) -> Result<(), Error>
{
if repeat < 1 {
repeat = 1;
}
for _ in 0..repeat {
let value = double()?;
println!("{}", ((start + (value * (end-start)))).round() as i64);
}
Ok(())
}
pub fn ceil(start: f64, end: f64, mut repeat: usize) -> Result<(), Error>
{
if repeat < 1 {
repeat = 1;
}
for _ in 0..repeat {
let value = double()?;
println!("{}", (start + (value * (end-start))).ceil() as i64);
}
Ok(())
}
pub fn floor(start: f64, end: f64, mut repeat: usize) -> Result<(), Error>
{
if repeat < 1 {
repeat = 1;
}
for _ in 0..repeat {
let value = double()?;
println!("{}", (start + (value * (end-start))).floor() as i64);
}
Ok(())
}

@ -0,0 +1,171 @@
#![allow(dead_code)]
extern crate getrandom;
use getrandom::*;
mod r#impl;
fn get<T: Default>() -> Result<T, Error>
{
let mut value: T = Default::default();
unsafe {
let mut slice = std::slice::from_raw_parts_mut(&mut value as *mut T as *mut u8, std::mem::size_of::<T>());
populate(&mut slice)?;
}
Ok(value)
}
fn double() -> Result<f64, Error>
{
let long: i64 = get::<i64>()?;
Ok( ((long & ((1i64 << 53) - 1)) as f64) * (1_f64 / ((1_i64 << 53) as f64)))
}
fn populate(mut value: &mut [u8]) -> Result<(), Error>
{
getrandom(&mut value)?;
Ok(())
}
fn int(low: i32, high: i32) -> Result<i32, Error>
{
let value = double()?;
return Ok(low + (value * (high - low) as f64).floor() as i32);
}
fn usage()
{
println!("Usage: rng --range <start> <end> [<repeat>]");
println!("Usage: rng --floor <start> <end> [<repeat>]");
println!("Usage: rng --round <start> <end> [<repeat>]");
println!("Usage: rng --ceil <start> <end> [<repeat>]");
println!("Usage: rng --of <opts...>");
println!("Usage: rng --shuffle <opts...>");
}
fn range(args: &[String]) -> Result<(), Error>
{
if args.len() < 2 {
usage();
} else {
let start: f64 = args[0].parse().expect("Not a number (start).");
let end: f64 = args[1].parse().expect("Not a number (end).");
if args.len() >= 3 {
let repeat: usize = args[2].parse().expect("Not a valid number (repeat).");
r#impl::range(start,end,repeat)?;
} else {
r#impl::range(start,end,1)?;
}
}
Ok(())
}
fn round(args: &[String]) -> Result<(), Error>
{
if args.len() < 2 {
usage();
} else {
let start: f64 = args[0].parse().expect("Not a number (start).");
let end: f64 = args[1].parse().expect("Not a number (end).");
if args.len() >= 3 {
let repeat: usize = args[2].parse().expect("Not a valid number (repeat).");
r#impl::round(start,end,repeat)?;
} else {
r#impl::round(start,end,1)?;
}
}
Ok(())
}
fn ceil(args: &[String]) -> Result<(), Error>
{
if args.len() < 2 {
usage();
} else {
let start: f64 = args[0].parse().expect("Not a number (start).");
let end: f64 = args[1].parse().expect("Not a number (end).");
if args.len() >= 3 {
let repeat: usize = args[2].parse().expect("Not a valid number (repeat).");
r#impl::ceil(start,end,repeat)?;
} else {
r#impl::ceil(start,end,1)?;
}
}
Ok(())
}
fn floor(args: &[String]) -> Result<(), Error>
{
if args.len() < 2 {
usage();
} else {
let start: f64 = args[0].parse().expect("Not a number (start).");
let end: f64 = args[1].parse().expect("Not a number (end).");
if args.len() >= 3 {
let repeat: usize = args[2].parse().expect("Not a valid number (repeat).");
r#impl::floor(start,end,repeat)?;
} else {
r#impl::floor(start,end,1)?;
}
}
Ok(())
}
fn of(args: &[String]) -> Result<(), Error>
{
let max = args.len();
let int = int(0, max as i32)? as usize;
println!("{}", args[int]);
Ok(())
}
fn shuffle(args: &[String]) -> Result<(), Error>
{
let mut values: Vec<String> = args.to_vec();
for i in (1..args.len()).rev() {
let j = int(0, i as i32)? as usize;
values.swap(i, j);
}
for a in values.iter() {
println!("{}", a);
}
Ok(())
}
fn main() -> Result<(), Error> {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
usage();
Ok(())
} else {
match args[1].to_lowercase().as_str() {
"--range" => range(&args[2..])?,
"--round" => round(&args[2..])?,
"--ceil" => ceil(&args[2..])?,
"--floor" => floor(&args[2..])?,
"--of" => of(&args[2..])?,
"--shuffle" => shuffle(&args[2..])?,
_ => usage()
};
Ok(())
}
}
Loading…
Cancel
Save