-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler_121.rs
94 lines (71 loc) · 1.99 KB
/
euler_121.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use core::panic;
use rand::prelude::*;
use rand_xoshiro::Xoshiro256Plus;
use rayon::prelude::*;
fn run_sim(payout: i64, max_turns: u32, iterations: u32) -> (f64, f64) {
let mut rng = Xoshiro256Plus::from_entropy();
let mut times_won = 0;
let mut avg_banker_cash = 0;
for _ in 0..iterations {
let mut score = 0;
let mut has_player_won = false;
for i in 0..max_turns as usize {
let player_wins = rng.gen_ratio(1, 2 + i as u32);
if player_wins {
score += 1;
}
// players has blue > red at this time
if score > max_turns / 2 {
has_player_won = true;
break;
}
}
if has_player_won {
times_won += 1;
avg_banker_cash -= payout - 1;
} else {
avg_banker_cash += 1;
}
}
(
times_won as f64 / iterations as f64,
avg_banker_cash as f64 / iterations as f64,
)
}
// 4 turn game = 11/120 = 0.09166666666666666
fn main() {
let mut rng = Xoshiro256Plus::from_entropy();
let lower_end = 2260;
let upper_end = 2270;
let mut payouts = (0..300)
.map(|_| rng.gen_range(lower_end..upper_end))
.collect::<Vec<i64>>();
payouts.sort();
payouts.dedup();
let turns = 15;
let n = 10_000_000;
let mut data = payouts
.par_iter()
.filter_map(|&payout| {
let (_, aba) = run_sim(payout, turns, n);
if aba < 0.0 {
return Some(payout);
}
None
})
.collect::<Vec<i64>>();
data.sort();
for payout in data {
let mut avg_banker_cash = 0.0;
for _ in 0..100 {
let (_, aba) = run_sim(payout, turns, n);
avg_banker_cash += aba;
}
avg_banker_cash /= 100.0;
if avg_banker_cash < 0.0 {
println!("Payout: {}", payout);
println!();
break;
}
}
}