Created
March 16, 2026 10:46
-
-
Save rbran/1a3242a3fef84fcd393b30c3444d719c to your computer and use it in GitHub Desktop.
Breaking three sticks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // inspired by https://www.youtube.com/watch?v=vR3b8N-YOLA | |
| // breaking a stick in two random points or | |
| // breaking a stick in one point and then breaking the other randonly | |
| // gives very diferent changes of forming a triangle with the three pices. | |
| use std::cmp::Ordering; | |
| use std::time::{Duration, SystemTime}; | |
| use rand; | |
| fn main() { | |
| let mut total = 0usize; | |
| let mut one_t = 0usize; | |
| let mut two_t = 0usize; | |
| let mut last_print = SystemTime::now(); | |
| loop { | |
| if two_cuts() { | |
| two_t += 1; | |
| } | |
| if one_cuts() { | |
| one_t += 1; | |
| } | |
| total += 1; | |
| let now = SystemTime::now(); | |
| let since_last_printed = now | |
| .duration_since(last_print) | |
| .unwrap_or(Duration::from_millis(0)); | |
| if since_last_printed > Duration::from_millis(1000 / 60) { | |
| last_print = now; | |
| print_total("one", one_t, total); | |
| print_total("two", two_t, total); | |
| } | |
| } | |
| } | |
| fn print_total(name: &str, num: usize, total: usize) { | |
| println!( | |
| "{name}: {:02.2}% => {num}/{total}", | |
| (num as f64 / total as f64) * 100.0 | |
| ); | |
| } | |
| fn two_cuts() -> bool { | |
| let cut1: f64 = rand::random_range(0.0..1.0); | |
| let cut2: f64 = rand::random_range(0.0..1.0); | |
| form_triangle(cut1, cut2) | |
| } | |
| fn one_cuts() -> bool { | |
| let cut1: f64 = rand::random_range(0.0..1.0); | |
| let cut2: f64 = if rand::random() { | |
| rand::random_range(0.0..cut1) | |
| } else { | |
| rand::random_range(cut1..1.0) | |
| }; | |
| form_triangle(cut1, cut2) | |
| } | |
| fn form_triangle(value1: f64, value2: f64) -> bool { | |
| let (first, second, third) = match value1.partial_cmp(&value2).unwrap() { | |
| Ordering::Less | Ordering::Equal => (value1, value2 - value1, 1.0 - value2), | |
| Ordering::Greater => (value2, value1 - value2, 1.0 - value1), | |
| }; | |
| first < 0.5 && second < 0.5 && third < 0.5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment