Das Galton-Brett – Java-Code
Ich möchte hier relativ unkommentiert (Im Quellcode befinden sich Anmerkungen auf Englisch) meine Version des Galton-Brettes in Java vorstellen.
Wer oder was das Galton-Brett ist, könnt ihr fix hier nachschauen, aber vielleicht reicht ja auch schon folgendes Bild um Erinnerungen wachzurufen:
Das Programm ist recht simpel. Die Variable height gibt die Anzahl der Reihen mit Nägeln an. x die Anzahl der Kugeln die geworfen werden sollen. Außerdem gibt scaleheight die Ausgabehöhe der “#” aus.
| java5 | | copy code | | ? |
| 01 | public class Galton {
|
| 02 | public static void main(String[] args) {
|
| 03 | // Initialize core-variables |
| 04 | int position; |
| 05 | int height = 10; // height of field |
| 06 | int x = 1000000; // number of balls |
| 07 | // Initialize output-variables |
| 08 | int highest = 0; // highest value to determine scalefactor |
| 09 | double scaleheight = 25.0; // max height of output |
| 10 | // Initialize Array for results |
| 11 | int boxes[] = new int[height + 1]; |
| 12 | /* |
| 13 | * throw x balls |
| 14 | */ |
| 15 | |
| 16 | for (int j = 0; j < x; j++) {
|
| 17 | position = 0; |
| 18 | for (int i = 0; i < height; i++) { // each ball has the chance of 50% to go left / right every time // If the height is the date of birth, we just set the chance to // go right to 100% if (i == 22) { position++; // or just leave out to invert } /* * Increment of position if e.g. the ball goes right. At the end * we know, the ball moved z times right, so he is in the zth * box from the left! */ else if (Math.random() >= 0.5) {
|
| 19 | position++; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // Increases the value of the box |
| 24 | boxes[position]++; |
| 25 | } |
| 26 | /* |
| 27 | * Prints the overall result |
| 28 | */ |
| 29 | for (int i = 0; i <= height="" i="" if="" boxes=""> boxes[highest]) {
|
| 30 | highest = i; |
| 31 | } |
| 32 | System.out.println("Box " + (i + 1) + " has " + boxes[i]
|
| 33 | + " balls in it."); |
| 34 | } |
| 35 | // make a scale factor to display large numbers |
| 36 | double scale = scaleheight / boxes[highest]; |
| 37 | // The Output loop |
| 38 | for (int j = 0; j < scaleheight; j++) {
|
| 39 | for (int i = 0; i < boxes.length; i++) { if (Math.round(scale * boxes[i]) >= scaleheight - j) {
|
| 40 | System.out.print("# ");
|
| 41 | } else {
|
| 42 | System.out.print(" ");
|
| 43 | } |
| 44 | } |
| 45 | System.out.println(); |
| 46 | } |
| 47 | for (int i = 0; i < boxes.length; i++) {
|
| 48 | if (i < 9) {
|
| 49 | System.out.print(0); |
| 50 | } |
| 51 | System.out.print(i + 1 + " "); |
| 52 | } |
| 53 | } |
| 54 | } |
Erweitern könnte man diesen Code noch durch eine Anzeige der Wahrscheinlichkeit. Außerdem ist die Ausgabe sicherlich schöner gestaltbar.

Montag, 23. Juli 2012 14:48
ES ist hier kein Quellcode auf dieser seite oder muss man den runterladen ?
Samstag, 28. Juli 2012 17:01
Hey Marko, danke für den Hinweis, es lag tatsächlich ein Versionsproblem mit einem Addon vor, welches nun behoben ist.
vg