fair 0.0.13

CLI tool and library for verifying provably fair games (baccarat, etc.).
Documentation
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! # provably fair plinko
//!

/*
For any game of Plinko, the generated outcome is based on the path of the falling ball. The
game event determines the direction of the falling ball for each level in the falling process.
Players can choose between 8 and 16 pins of play, which determines the number of game events
required to generate a complete path from top to bottom. Since there are only two possible
directions (left or right) the translation is done by multiplying each float by 2, which maps
to the following index:

// Index of 0 to 1 : left to right
const DIRECTIONS = [ left, right ];
// Game event translation
const direction = CARDS[Math.floor(float * 2)];
*/

pub use crate::rng::{ProvablyFairConfig, ProvablyFairRNG};
use std::fmt;

#[derive(Debug)]
pub struct SimulationResult {
    pub payout: f64,
    pub index: usize,
}

impl fmt::Display for SimulationResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Slot: {}\nPayout: {}x", self.index + 1, self.payout)
    }
}

#[derive(Debug, Copy, Clone)]
pub enum Risk {
    Low,
    Medium,
    High,
}
impl Risk {
    pub fn from_str(s: &str) -> Risk {
        match &s.to_lowercase()[..] {
            "low" => Risk::Low,
            "medium" => Risk::Medium,
            "high" => Risk::High,
            _ => panic!("invalid risk string {}", s),
        }
    }
}

pub struct Opts {
    risk: Risk,
    rows: u8,
}

impl Opts {
    pub fn default() -> Opts {
        Self::new(8, Risk::Low)
    }
    pub fn new(rows: u8, risk: Risk) -> Opts {
        assert!(rows >= 8);
        assert!(rows <= 16);
        Opts { risk, rows }
    }
}

#[derive(Copy, Clone)]
enum Direction {
    Left,
    Right,
}

use Direction::*;

fn get_direction(rng: &mut ProvablyFairRNG<f64>) -> Direction {
    let directions = [Left, Right];
    directions[(rng.next().unwrap() * 2.) as usize]
}

/// Simulates a game of diamond poker.
///
/// # Example
///
/// ```
/// use fair::{games, ProvablyFairConfig};
///
/// let config = ProvablyFairConfig::new("some client seed", "some server seed", 1);
/// let result = games::plinko::simulate(config, None);
/// ```
///
pub fn simulate(config: ProvablyFairConfig, opts: Option<Opts>) -> SimulationResult {
    let mut rng: ProvablyFairRNG<f64> = ProvablyFairRNG::from_config(config);
    let opts = opts.unwrap_or(Opts::default());

    let total: usize = (3 + opts.rows as usize) * 2 - 1;
    let middle: usize = (total / 2) + 1;

    let mut idx: i32 = middle as i32;
    for _ in 0..opts.rows {
        idx += match get_direction(&mut rng) {
            Left => -1,
            Right => 1,
        }
    }
    idx = idx / 2 - 1;

    let payout = slot_payout(opts.rows as usize, &opts.risk, idx as usize);

    SimulationResult {
        payout,
        index: idx as usize,
    }
}

// Payout matrixes
static PAYOUT_8: [[f64; 5]; 3] = [
    // low risk
    [5.6, 2.1, 1.1, 1., 0.5],
    // medium risk
    [13., 3., 1.3, 0.7, 0.4],
    // high risk
    [29., 4., 1.5, 0.3, 0.2],
];
static PAYOUT_9: [[f64; 5]; 3] = [
    // low risk
    [5.6, 2., 1.6, 1., 0.7],
    // medium risk
    [18., 4., 1.7, 0.9, 0.5],
    // high risk
    [43., 7., 2., 0.6, 0.2],
];
static PAYOUT_10: [[f64; 6]; 3] = [
    // low risk
    [8.9, 3., 1.4, 1.1, 1., 0.5],
    // medium risk
    [22., 5., 2., 1.4, 0.6, 0.4],
    // high risk
    [76., 10., 3., 0.9, 0.3, 0.2],
];
static PAYOUT_11: [[f64; 6]; 3] = [
    // low risk
    [8.4, 3., 1.9, 1.3, 1., 0.7],
    // medium risk
    [24., 6., 3., 1.8, 0.7, 0.5],
    // high risk
    [120., 14., 5.2, 1.4, 0.4, 0.2],
];
static PAYOUT_12: [[f64; 7]; 3] = [
    // low risk
    [10., 3., 1.6, 1.4, 1.1, 1., 0.5],
    // medium risk
    [33., 11., 4., 2., 1.1, 0.6, 0.3],
    // high risk
    [170., 24., 8.1, 2., 0.7, 0.2, 0.2],
];
static PAYOUT_13: [[f64; 7]; 3] = [
    // low risk
    [8.1, 4., 3., 1.9, 1.2, 0.9, 0.7],
    // medium risk
    [43., 13., 6., 3., 1.3, 0.7, 0.4],
    // high risk
    [260., 37., 11., 4., 1., 0.2, 0.2],
];
static PAYOUT_14: [[f64; 8]; 3] = [
    // low risk
    [7.1, 4., 1.9, 1.4, 1.3, 1.1, 1., 0.5],
    // medium risk
    [58., 15., 7., 4., 1.9, 1., 0.5, 0.2],
    // high risk
    [420., 56., 18., 5., 1.9, 0.3, 0.2, 0.2],
];
static PAYOUT_15: [[f64; 8]; 3] = [
    // low risk
    [15., 8., 3., 2., 1.5, 1.1, 1., 0.7],
    // medium risk
    [88., 18., 11., 5., 3., 1.3, 0.5, 0.3],
    // high risk
    [620., 83., 27., 8., 3., 0.5, 0.2, 0.2],
];
static PAYOUT_16: [[f64; 9]; 3] = [
    // low risk
    [16., 9., 2., 1.4, 1.4, 1.2, 1.1, 1., 0.5],
    // medium risk
    [110., 41., 10., 5., 3., 1.5, 1., 0.5, 0.3],
    // high risk
    [1000., 130., 26., 9., 4., 2., 0.2, 0.2, 0.2],
];

fn slot_payout(rows: usize, risk: &Risk, slot_index: usize) -> f64 {
    let risk_idx = match risk {
        Risk::Low => 0,
        Risk::Medium => 1,
        Risk::High => 2,
    };

    let payout_row = match rows {
        8 => &PAYOUT_8[risk_idx][..],
        9 => &PAYOUT_9[risk_idx][..],
        10 => &PAYOUT_10[risk_idx][..],
        11 => &PAYOUT_11[risk_idx][..],
        12 => &PAYOUT_12[risk_idx][..],
        13 => &PAYOUT_13[risk_idx][..],
        14 => &PAYOUT_14[risk_idx][..],
        15 => &PAYOUT_15[risk_idx][..],
        16 => &PAYOUT_16[risk_idx][..],
        _ => panic!("rows ({}) must be between 8 and 16 inclusive", rows),
    };

    let slot_index = slot_index as i32;
    let len = payout_row.len() as i32;
    let last_idx = len - 1;
    // Passed the last payout in the array, we go from right to left because the payouts are
    // symmetric.
    let index = if slot_index > last_idx {
        // The middle payout for 8, 10, 12, 14, 16 rows is not repeated
        // For example for 8 rows, payout row is (1 is not repeated):
        // 5.6, 2.1, 1.1, 1, 0.5, 1, 1.1, 2.1, 5.6
        // Whereas for 9 rows, payout row is (0.7 is repeated):
        // 5.6, 2, 1.6, 1, 0.7, 0.7, 1, 1.6, 2, 5.6
        let repeat = if rows % 2 == 0 { 0 } else { 1 };
        last_idx - (slot_index - last_idx) + repeat
    } else {
        slot_index
    } as usize;
    payout_row[index]
}

fn fac(n: u32) -> u32 {
    let mut i = n;
    let mut res = 1;
    while i > 0 {
        res = res * i;
        i -= 1;
    }
    res
}

use num_integer;

fn slot_probability(rows: usize, slot_index: usize) -> f64 {
    // https://en.wikipedia.org/wiki/Bean_machine#Distribution_of_the_beads
    let p: f64 = 0.5;

    let n = rows as f64;
    let k = slot_index;
    let binom = num_integer::binomial(rows, k) as f64;
    let k = k as f64;

    let prob = binom as f64 * p.powf(k) * (1. - p).powf(n - k);
    prob
}

fn compute_expected_value(rows: usize, risk: &Risk) -> f64 {
    let total_slots = rows + 1;
    (0..total_slots).fold(0., |acc, idx| {
        let payout = slot_payout(rows, risk, idx);
        let probability = slot_probability(rows, idx);
        /*
        println!(
            "({}, {:?}, {}) payout: {} probability: {}",
            rows, risk, idx, payout, probability
        );
        */
        let expected = payout * probability;
        acc + expected
    })
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn simulate_plinko_test() {
        let config = ProvablyFairConfig::new("client seed", "server seed", 1);
        assert_eq!(simulate(config, None).index, 7);
        let config = ProvablyFairConfig::new("client seed", "server seed", 2);
        assert_eq!(simulate(config, None).index, 2);
        let config = ProvablyFairConfig::new("client seed", "server seed", 3);
        assert_eq!(simulate(config, None).index, 5);
        let config = ProvablyFairConfig::new("client seed", "server seed", 1);
        assert_eq!(simulate(config, Some(Opts::new(9, Risk::Low))).index, 8);
        let config = ProvablyFairConfig::new("client seed", "server seed", 2);
        assert_eq!(simulate(config, Some(Opts::new(9, Risk::Low))).index, 3);
        let config = ProvablyFairConfig::new("client seed", "server seed", 3);
        assert_eq!(simulate(config, Some(Opts::new(9, Risk::Low))).index, 6);
    }

    /*
    #[test]
    fn test_expected_values_once() {
        let rows = 9;
        let risk = Risk::Low;
        let expected_value = compute_expected_value(rows, &risk);
        println!(
            "Expected value: {:.2}% House Edge: {:.2}% (rows: {}, risk: {:?})",
            expected_value * 100.,
            (1. - expected_value) * 100.,
            rows,
            risk
        );
    }
    */
    #[test]
    fn test_expected_values() {
        let possible_rows: &[usize] = &[8, 9, 10, 11, 12, 13, 14, 15, 16];
        let possible_risks: &[Risk] = &[Risk::Low, Risk::Medium, Risk::High];
        for &rows in possible_rows {
            for risk in possible_risks {
                let expected_value = compute_expected_value(rows, risk);
                let house_edge = (1. - expected_value) * 100.;
                assert!(house_edge > 0.8 && house_edge < 1.2);
            }
        }
    }

    #[test]
    fn simulate_plinko_test_payout() {
        assert_eq!(
            simulate(
                ProvablyFairConfig::new("client seed", "server seed", 1),
                Some(Opts::new(16, Risk::Low))
            )
            .payout,
            1.4
        );
        assert_eq!(
            simulate(
                ProvablyFairConfig::new("client seed", "server seed", 1),
                Some(Opts::new(8, Risk::Low))
            )
            .payout,
            2.1
        );
        assert_eq!(
            simulate(
                ProvablyFairConfig::new("client seed", "server seed", 1),
                Some(Opts::new(8, Risk::Medium))
            )
            .payout,
            3.
        );
        assert_eq!(
            simulate(
                ProvablyFairConfig::new("client seed", "server seed", 1),
                Some(Opts::new(8, Risk::High))
            )
            .payout,
            4.
        );
        assert_eq!(
            simulate(
                ProvablyFairConfig::new("client seed", "server seed", 1),
                Some(Opts::new(9, Risk::Low))
            )
            .payout,
            2.
        );
        assert_eq!(
            simulate(
                ProvablyFairConfig::new("client seed", "server seed", 1),
                Some(Opts::new(9, Risk::Low))
            )
            .payout,
            2.
        );
        assert_eq!(
            simulate(
                ProvablyFairConfig::new("client seed", "server seed", 1),
                Some(Opts::new(9, Risk::Medium))
            )
            .payout,
            4.
        );
        assert_eq!(
            simulate(
                ProvablyFairConfig::new("client seed", "server seed", 1),
                Some(Opts::new(10, Risk::Low))
            )
            .payout,
            1.4
        );
    }
    #[test]
    fn test_fac() {
        assert_eq!(fac(3), 6);
        assert_eq!(fac(4), 24);
        assert_eq!(fac(5), 120);
    }
    #[test]
    fn test_binomial_coefficient() {
        /*
        for i in 1..8 {
            println!("{}", num_integer::binomial(8, i));
        }
        */
        assert_eq!(num_integer::binomial(52, 5), 2_598_960);
    }
    #[test]
    fn test_slot_probability() {
        assert_eq!(slot_probability(8, 0), 0.00390625);
        assert_eq!(slot_probability(8, 1), 0.03125);
        assert_eq!(slot_probability(8, 2), 0.109375);
        assert_eq!(slot_probability(8, 3), 0.218750);
        assert_eq!(slot_probability(8, 4), 0.2734375);
        assert_eq!(slot_probability(8, 5), 0.218750);
        assert_eq!(slot_probability(8, 6), 0.109375);
        assert_eq!(slot_probability(8, 7), 0.03125);
        assert_eq!(slot_probability(8, 8), 0.00390625);

        assert_eq!(slot_probability(9, 0), 0.001953125);
        assert_eq!(slot_probability(9, 1), 0.017578125);
        assert_eq!(slot_probability(9, 2), 0.0703125);
        assert_eq!(slot_probability(9, 3), 0.1640625);
        assert_eq!(slot_probability(9, 4), 0.24609375);
        assert_eq!(slot_probability(9, 5), 0.24609375);
        assert_eq!(slot_probability(9, 6), 0.1640625);
        assert_eq!(slot_probability(9, 7), 0.0703125);
        assert_eq!(slot_probability(9, 8), 0.017578125);
        assert_eq!(slot_probability(9, 9), 0.001953125);
    }
}