Skip to content

Instantly share code, notes, and snippets.

@tallpeak
Created September 8, 2022 22:57
Show Gist options
  • Save tallpeak/abb1ed395997a455af3e3c50f968ea67 to your computer and use it in GitHub Desktop.
Save tallpeak/abb1ed395997a455af3e3c50f968ea67 to your computer and use it in GitHub Desktop.
// give.a.dollar.rs, from julia, from tiny-c:
// http://primepuzzle.com/not.just.tiny.c/give.a.dollar.try.tc?fbclid=IwAR2p5YBQgJJDY3-g3lN3rRHw7v7cA0VB0BcNWv4dHybH-XCCgxQQIrbF6sY
// ref: https://www.facebook.com/groups/299317782048/posts/10160467717107049/
use std::io;
use rand::prelude::*;
use timed::timed; //https://y2kappa.github.io/blog/posts/timing-your-function-execution/
fn gn() -> i32 {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_n_size) => {
let n : i32 = input.trim().parse::<i32>().unwrap();
return n;
}
Err(error) => {
println!("error: {error}");
return 0;
}
}
}
#[timed]
fn givadollar(limit : i32) -> () {
let mut rng = thread_rng();
let mut dollar = vec![45 as i32; 45];
for _j in 0..limit {
for i in 0..45 {
if dollar[i] > 0 { // can't give if you don't have
let mut rn = rng.gen_range(0..=43);
if rn >= i {
rn+=1;
}
dollar[rn]+=1; // # give a random person $1
dollar[i]-=1; // and deduct $1 from the giver
}
} // i
} // j
for i in 0..45 { // display results
print!("{:3} ", dollar[i]);
}
}
fn main() {
println!("give-a-dollar TC version by lrb - 9/3/22 -- translation to Rust by AWW 9/8/22");
let mut limit = 0;
//let mut seed = 0; // dont feel like learning how to use SeedableRng
if limit == 0 {
print!("number of rounds, e.g. 50..10000 : ");
limit = gn();
// print!("seed please (0 for no seed) : ");
// let seed = gn();
// if seed != 0 {
// }
}
givadollar(limit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment