Last active
August 29, 2015 14:15
-
-
Save stefanoc/7b33c39ffea1f7b3f3e2 to your computer and use it in GitHub Desktop.
Dadi
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
require 'pp' | |
dice = [0, 0, 1, 2, 3, 4] | |
throws = dice.product(dice, dice, dice, dice) | |
counts = throws.inject(Hash.new(0)) { |t, p| t[p.inject(&:+)] += 1; t } | |
pp counts.map { |(k, v)| [k, (v.fdiv(throws.length) * 100).round(3)] } |
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
#![feature(core)] | |
use std::collections::BTreeMap; | |
use std::iter::AdditiveIterator; | |
fn v_product<'a, T: Clone>(ref x: &'a [T], ref y: &'a [T]) -> Vec<Vec<T>> { | |
let mut result = vec![]; | |
for _x in x.iter() { | |
for _y in y.iter() { | |
result.push(vec![_x.clone(), _y.clone()]); | |
} | |
} | |
result | |
} | |
fn n_product<'a, T: Clone>(ref x: Vec<Vec<T>>, ref y: &'a [T]) -> Vec<Vec<T>> { | |
let mut result = vec![]; | |
for _x in x.iter() { | |
for _y in y.iter() { | |
let mut _z = _x.clone(); | |
_z.push(_y.clone()); | |
result.push(_z); | |
} | |
} | |
result | |
} | |
fn main() { | |
let dice = &[0, 0, 1, 2, 3, 4]; | |
let throws = n_product(n_product(n_product(v_product(dice, dice), dice), dice), dice); | |
let mut counts = BTreeMap::<usize, usize>::new(); | |
for t in throws.iter() { | |
let k = t.iter().cloned().sum(); | |
let c = match counts.get(&k) { | |
None => 1, | |
Some(x) => *x + 1 | |
}; | |
counts.insert(k, c); | |
} | |
for (c, v) in counts.iter() { | |
println!("{}: {}", c, (*v as f32) / (throws.len() as f32) * 100.0); | |
} | |
} |
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
[[0, 0.412], | |
[1, 1.029], | |
[2, 2.058], | |
[3, 3.601], | |
[4, 5.787], | |
[5, 7.729], | |
[6, 9.581], | |
[7, 10.995], | |
[8, 11.638], | |
[9, 11.124], | |
[10, 10.044], | |
[11, 8.423], | |
[12, 6.494], | |
[13, 4.565], | |
[14, 3.022], | |
[15, 1.813], | |
[16, 0.965], | |
[17, 0.45], | |
[18, 0.193], | |
[19, 0.064], | |
[20, 0.013]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment