Skip to content

Instantly share code, notes, and snippets.

@nicolaibach
Created October 22, 2023 19:37
Show Gist options
  • Save nicolaibach/3a624a8c177caecec8d4a4082d92b4dc to your computer and use it in GitHub Desktop.
Save nicolaibach/3a624a8c177caecec8d4a4082d92b4dc to your computer and use it in GitHub Desktop.

Würfel Experiment

Wie hoch ist die Wahrscheinlichkeit bei einem Wurf mit 2 Würfeln eine gerade bzw. ungerade Gesamtzahl zu würfeln?
— PD02a Grundlagen der Mathematik | Folien zu Seminar 1

TL;DR

Bei einem Wurf mit 2 Würfeln (mit jeweils 6 Seiten) ist die Wahrscheinlichkeit eine gerade bzw. ungerade Gesamtzahl zu würfeln 50/50.

Test Ergebnisse

dice sides total even odd equal
1 2 2 1 1 true
1 3 3 1 2 false
1 4 4 2 2 true
1 5 5 2 3 false
1 6 6 3 3 true
2 2 4 2 2 true
2 3 9 5 4 false
2 4 16 8 8 true
2 5 25 13 12 false
2 6 36 18 18 true
3 2 8 4 4 true
3 3 27 13 14 false
3 4 64 32 32 true
3 5 125 62 63 false
3 6 216 108 108 true

Fazit

Für die Verteilung gerader und ungerader Ergebnisse spielt die Anzahl der Würfel keine Rolle. Die Anzahl der Seiten ist entscheidend. Die Verteilung gerader und ungerader Ergebnisse entspricht der Verteilung gerader und ungerader Zahlen auf den Würfeln.

Siehe auch

/**
* Create an array of numbers from 1 to `length`
*
* @param {integer} length
* @returns {[integer]} The list of numbers
*/
const range = (length) => Array.from({length}, (_, i) => i + 1);
/**
* Create an array of size `length` filled with `thing`s
*
* @param {integer} length
* @param {*} thing
* @returns {[*]}
*/
const fillArray = (length, thing) => Array.from({length}).fill(thing);
/**
* Generate the cartesian product of the given `iterables`
*
* Based on this [SO answer by @le_me](https://stackoverflow.com/a/44338759)
*
* @param {[iterable]} list of iterables
* @yields {array}
*/
function* cartesian([head, ...tail]) {
const remainder = tail.length > 0 ? cartesian(tail) : [[]];
for (const r of remainder)
for (const h of head)
yield [h, ...r];
}
/**
* Calculate the total value of numbers in the given `list`
*
* @param {[number]} list of numbers to sum
* @returns {number}
*/
const sum = (list) => list.reduce((a, b) => a + b, 0);
/**
* Check if the given number `n` is even
*
* @param {number} n
* @returns {boolean} `true` if `n` is even, false otherwise
*/
const isEven = (n) => n % 2 === 0;
/**
* Generate some dice and check the distribution of
* even and odd results
*
* @param {integer} dice The number of dice
* @param {integet} sides The number of sides per dice
* @returns {object}
*/
export const rollDice = (dice, sides) => {
const stack = fillArray(dice, range(sides));
const values = [...cartesian(stack)].map(sum);
const total = values.length;
const even = values.filter(isEven).length;
const odd = total - even;
const equal = even === odd;
return {
dice,
sides,
total,
even,
odd,
equal,
};
}
import { rollDice } from './roll-dice.js';
const tests = [
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[1, 6],
[2, 2],
[2, 3],
[2, 4],
[2, 5],
[2, 6],
[3, 2],
[3, 3],
[3, 4],
[3, 5],
[3, 6],
// [10, 10] Don't do this!
// --> FATAL ERROR: Reached heap limit Allocation failed
];
const results = tests.map((options) => rollDice(...options));
console.table(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment