Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

// Programm: muenzwurf.C // berechnet eine Folge von Pseudozufallszahlen, // simuliert damit Muenzwuerfe und ueberprueft // deren Verteilung statistisch #include int main() { // Lineare Kongruenzmethode zur Erzeugung von // Pseudozufallszahlen x_0, x_1, x_2, ... // mit Hilfe der Formel x_i = a * x_(i-1) % m const unsigned int m = 65536; // Modulus, 2^16 const unsigned int a = 47485; // Multiplikator const unsigned int x0 = 1; // Startwert // lies Anzahl zu werfender Muenzen ein std::cout << "Anzahl von Muenzwuerfen: "; unsigned int anzahl; std::cin >> anzahl; // Erzeugung und Ausgabe unsigned int x = x0; // aktuelle Zufallszahl std::cout << "Folge von " << anzahl << " Muenzwuerfen: "; unsigned int kopf = 0; // # "Kopf"-Wuerfe unsigned int zahl = 0; // # "Zahl"-Wuerfe while (anzahl > 0) { // fuehrendes Bit der Zufallszahl unsigned int muenzwurf = x / (m / 2); std::cout << muenzwurf << " "; if (muenzwurf == 0) kopf = kopf + 1; else zahl = zahl + 1; x = a * x % m; anzahl = anzahl - 1; } std::cout << "\nKopf: " << kopf << "-mal" << "\nZahl: " << zahl << "-mal" << std::endl; return 0; }