Skip to content

Instantly share code, notes, and snippets.

@stefanoc
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanoc/7b33c39ffea1f7b3f3e2 to your computer and use it in GitHub Desktop.
Save stefanoc/7b33c39ffea1f7b3f3e2 to your computer and use it in GitHub Desktop.
Dadi
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)] }
#![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);
}
}
[[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