Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate rand;
- use rand::Rng;
- type Lotto = [i8;7];
- fn create_lotto<R: Rng> (rng: &mut R) -> Lotto
- {
- let mut res = [0;7];
- for i in 0..7 {
- loop {
- let val = rng.gen_range(1 as i8, 45 as i8);
- if !res[0..i].contains(&val) {
- res[i] = val;
- break;
- }
- }
- }
- res.sort();
- res
- }
- fn dup_n_range_k(n: i8, k: i8) -> Box<Fn(Lotto, Lotto) -> bool>
- {
- Box::new(move |prev, next| {
- let mut same = 0;
- let mut range = 0;
- for &value in next.iter() {
- if prev.contains(&value) {
- same += 1;
- }
- if prev.iter()
- .any(|&x| x != value && x >= (value-k) && x <= (value+k))
- {
- range += 1;
- }
- }
- n == same && range == k
- })
- }
- fn try<R: Rng> (rng: &mut R, f: Box<Fn(Lotto, Lotto)->bool>, n: i32)
- -> usize
- {
- let mut count = 0;
- let mut prev = create_lotto(rng);
- for _ in 0..n {
- let new = create_lotto(rng);
- if f(prev, new) {
- count += 1;
- }
- prev = new;
- }
- return count;
- }
- fn main() {
- let mut rng = rand::thread_rng();
- let mut accum = Vec::new();
- let n = 10000;
- for _ in 0..n {
- let count = try(&mut rng, dup_n_range_k(5, 1), 744) ;
- if accum.len() <= count {
- accum.resize(count +1, 0);
- }
- accum[count] += 1;
- }
- println!("숫자 5개가 직전회차에 그대로 발견되고, 나머지 두 숫자는 직전회차 숫자와 +-1 이내일 확률: {:.3}%", (accum[1] as f64)/(n as f64) * 100.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement